I needed to modify a pdf the other day, the first and last pages needed to be removed.
So I did what anyone would do, I made a Perl program to do it for me.
Then I was so happy tweeted about it.
Olivier Mengué asked what module was used for this. "PDF::API2" but just telling what module seems too easy so I will show you.
First a little confession I didn't write the code from scratch in just a minute, I took an existing program I had that combines two pdf files into one.
This program has some issues, the filenames are hard coded and I really don't care for the indexed for loop, but it works great and fills its need well. Here it is for the curious:
It was almost right to do the trimming so I jiggled things around a little and came up with this:
That is slightly better than the original, couldn't resist just a little clean up. But can it be better?
After a little more rework:
It is now more generic, it takes arguments from the command line for file names. The double loop was cleaned up since it will only process a single set of files at a time. The range operator replaces the indexed for loop. Finally the subroutine was inlined it wasn't adding much anymore so I dropped it.
After I posted the tweet Perl came up as being hard to read, I am interested to know what you think. This is just a short program but it seems pretty readable to me. (the last one is anyway) Certainly not all of my code comes out this clean but not all is going to be maintained either. What do you think?
Quick note on Perl modules for working with pdf files: I used "PDF::API2" for this task where I am manipulating full pages, either putting them together or taking pages out. When I am creating pdfs (putting text and lines onto a page) I have been using "PDF::Create" also in the past I have been known to just write out postscript and send it through ps2pdf. Want samples? Just ask and I will post a few.
Thursday, October 31, 2019
Wednesday, October 3, 2018
Code & Coffee for October 3, 2018
Here is what you missed:
What is a `trie` and the genesis of this insomnia driven tweet: https://twitter.com/Hopasaurus/status/1047363507736498176
A lot more about `tries` pretty sure Othelle was thinking I fell off the deep end.
A review of linked list traversal.
A potential match opportunity for a searching developer and an empty position (I really hope there is more to talk about on this front soon)
We also talked about
Spring boot:
http://spring.io/projects/spring-boot
https://spring.io/guides/gs/spring-boot/
Programming interviews, specifically “Programming Interviews Exposed”:
http://www.wrox.com/WileyCDA/WroxTitle/Programming-Interviews-Exposed-Secrets-to-Landing-Your-Next-Job-2nd-Edition.productCd-047012167X.html
$20-ish on Amazon: https://www.amazon.com/Programming-Interviews-Exposed-Through-Interview/dp/111941847X/ref=sr_1_2?ie=UTF8&qid=1538572735&sr=8-2&keywords=programming+interviews+exposed
The Cormen “Introduction to Algorithms” book:
https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press/dp/0262033844/ref=sr_1_3?ie=UTF8&qid=1538572820&sr=8-3&keywords=algorithms
I don't have this one yet, but do have the Sedgewick and Skiena books and we compared and contrasted those.
On the topic of algorithms, this is on my reading list: http://algorithmstoliveby.com/ it looks really interesting, and looks like does an interesting job of drawing parallels between algorithms and real life experiences. If you decide to check it out, please let me know what you think of it.
Up next week:
I am developing some sorting games that we can try out, would be interesting to get some feedback to see if they seem useful and warrant further development.
Perhaps a presentation on linked list traversal?
Also open to suggestions
What is a `trie` and the genesis of this insomnia driven tweet: https://twitter.com/Hopasaurus/status/1047363507736498176
A lot more about `tries` pretty sure Othelle was thinking I fell off the deep end.
A review of linked list traversal.
A potential match opportunity for a searching developer and an empty position (I really hope there is more to talk about on this front soon)
We also talked about
Spring boot:
http://spring.io/projects/spring-boot
https://spring.io/guides/gs/spring-boot/
Programming interviews, specifically “Programming Interviews Exposed”:
http://www.wrox.com/WileyCDA/WroxTitle/Programming-Interviews-Exposed-Secrets-to-Landing-Your-Next-Job-2nd-Edition.productCd-047012167X.html
$20-ish on Amazon: https://www.amazon.com/Programming-Interviews-Exposed-Through-Interview/dp/111941847X/ref=sr_1_2?ie=UTF8&qid=1538572735&sr=8-2&keywords=programming+interviews+exposed
The Cormen “Introduction to Algorithms” book:
https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press/dp/0262033844/ref=sr_1_3?ie=UTF8&qid=1538572820&sr=8-3&keywords=algorithms
I don't have this one yet, but do have the Sedgewick and Skiena books and we compared and contrasted those.
On the topic of algorithms, this is on my reading list: http://algorithmstoliveby.com/ it looks really interesting, and looks like does an interesting job of drawing parallels between algorithms and real life experiences. If you decide to check it out, please let me know what you think of it.
Up next week:
I am developing some sorting games that we can try out, would be interesting to get some feedback to see if they seem useful and warrant further development.
Perhaps a presentation on linked list traversal?
Also open to suggestions
Wednesday, September 19, 2018
Code and Coffee September 19, 2018
Here in Grand Rapids a few of us regularly get together over coffee and talk about code.
Here is a report that I sent back to our slack group to tell about what we did and I thought it might be good to share it here so I can point people somewhere when they ask what it is about:
Code & Coffee was pretty epic this morning. I talked so much it took nearly three hours to finish my sandwich. The theme of today turned out to be Algorithms. We started talking about Codepen.io, and ACE editor which somehow go onto the topic of algorithms which it turns out we have both been brushing up on in the last few weeks.
We did light coverage a couple of sorting algorithms insertion and selection sorts) going through my notes and a run through of each on some sample data. Talked about the runtime ( O(n^2) ) then we talked about other sorting including 3 way quick sort which can get to linearithmic time with certain (likely) key distributions. (Also quick sort works well with modern caching architectures which is a free bonus performance boost)
Then we wandered off into linked lists and doubly linked lists, heaps and binary search trees. Finishing up with finding the middle of a linked list in one pass. All in all it was a good discussion @Othelle allowed me to practice talking about the things made notes on and hopefully it was at least entertaining for him if not educational too.
Links: https://codepen.io/
https://ace.c9.io/
http://www.algorist.com/
https://algs4.cs.princeton.edu/home/
Somewhere along the line we talked about Conway’s Game of Life
here are two implementations I did (one only gets partial credit: https://github.com/Hopasaurus/GameOfLife (in C# partially done, check the commits for the interesting bit of this)
https://www.youtube.com/watch?v=iT7b59CmUjk (in C for Arduino, code on request if I can find it back)
Also my (one) post for last year was a somewhat related topic: http://blog.hopasaurus.com/2017/10/tail-recursion-considered-harmful.html
Here is a report that I sent back to our slack group to tell about what we did and I thought it might be good to share it here so I can point people somewhere when they ask what it is about:
Code & Coffee was pretty epic this morning. I talked so much it took nearly three hours to finish my sandwich. The theme of today turned out to be Algorithms. We started talking about Codepen.io, and ACE editor which somehow go onto the topic of algorithms which it turns out we have both been brushing up on in the last few weeks.
We did light coverage a couple of sorting algorithms insertion and selection sorts) going through my notes and a run through of each on some sample data. Talked about the runtime ( O(n^2) ) then we talked about other sorting including 3 way quick sort which can get to linearithmic time with certain (likely) key distributions. (Also quick sort works well with modern caching architectures which is a free bonus performance boost)
Then we wandered off into linked lists and doubly linked lists, heaps and binary search trees. Finishing up with finding the middle of a linked list in one pass. All in all it was a good discussion @Othelle allowed me to practice talking about the things made notes on and hopefully it was at least entertaining for him if not educational too.
Links: https://codepen.io/
https://ace.c9.io/
http://www.algorist.com/
https://algs4.cs.princeton.edu/home/
Somewhere along the line we talked about Conway’s Game of Life
here are two implementations I did (one only gets partial credit: https://github.com/Hopasaurus/GameOfLife (in C# partially done, check the commits for the interesting bit of this)
https://www.youtube.com/watch?v=iT7b59CmUjk (in C for Arduino, code on request if I can find it back)
Also my (one) post for last year was a somewhat related topic: http://blog.hopasaurus.com/2017/10/tail-recursion-considered-harmful.html
Thursday, October 26, 2017
Tail Recursion considered harmful?
Some time back, someone told me that tail recursion is a lot like using a 'goto' this makes a lot of sense when you think about it. Tail recursion is an optimization that can happen when a function is calling itself as and returning the result as it's return. This allows the system to skip creating a new frame on the stack and makes life a little easier when fiddling with limited stack while doing embedded work.
An example might help:
First we should look at the paper... more than just the title of course but first we can focus on the title. Kind of an interesting thing happened with the title. The writer of the paper did not chose it the editor of the publishing journal did. Turns out the original title was "A case against the goto statement."
To the editor, this title was not quite right so he changed it to: "Go To Statement Considered Harmful." Sounds like clickbait to me. Oh, wait, it is. Turns out that way before 'clickbait' was a thing people had to sell the news (that is convince people to part with hard earned money for a physical paper, how quaint.)
According to my hastily googled up source; 1949 gave us this: "Rent Control Controversy / Enacting Now of Hasty Legislation Considered Harmful" sounds exciting right? That is from the national news. It looks like the "Go To" title started a bit of a theme in CS articles: "Global variables considered harmful" and "The letter O considered harmful."
Global variables I can see, and wish that the message got out more on that, but really, the letter O? (Let me tell you it was a real problem, 0 and O didn't always look so different. In fact my typewriter doesn't even have a zero, gotta use the O to make a zero so yeah the problem is real. See also l and 1 bet you have to squint to figure out which is 'ell' and which is 'one', 1 would tell you but 1 already forgot. (Are we sure computers use Ones and Zeros or do they use Ohs and Ells?))
Is Tail Recursion harmful?
I think that is the wrong question. I think the real question is: Is Tail Recursion the same as using a Goto? Which I think the answer is no. Earlier today I thought maybe they were the same, after thinking and reading Dijkstra's article again which calls recursion in place of goto I am more convinced that goto can go.
Gotos replacing tail recursion are a gateway to mass hysteria. Gotos everywhere, jumping into the middle of a function with no way to know how you got there or where you came from. Sounds like I am exaggerating, maybe I am, but not by much (been there and done that it's not pretty.)
Links to more on this:
https://en.wikipedia.org/wiki/Considered_harmful
https://en.wikipedia.org/wiki/Niklaus_Wirth
https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form
https://en.wikipedia.org/wiki/Syntax_diagram
One last thing:
I had trouble deciding on the title for this the alternate is
"Considered Harmful" Considered Harmful ... see what I did there? A little recursion in the title. Let me know in the comments which title you would have picked.
You can also read about this here: Tail Recursion considered harmful?
An example might help:
So the code compiles and both do what it looks like they do. What's the big deal? And isn't 'goto' bad? Everyone knows it right? What about that famous paper and everything. It sure looks like recursion is just a fancy way to do a goto.
Lets dig into it a little:
First we should look at the paper... more than just the title of course but first we can focus on the title. Kind of an interesting thing happened with the title. The writer of the paper did not chose it the editor of the publishing journal did. Turns out the original title was "A case against the goto statement."
To the editor, this title was not quite right so he changed it to: "Go To Statement Considered Harmful." Sounds like clickbait to me. Oh, wait, it is. Turns out that way before 'clickbait' was a thing people had to sell the news (that is convince people to part with hard earned money for a physical paper, how quaint.)
According to my hastily googled up source; 1949 gave us this: "Rent Control Controversy / Enacting Now of Hasty Legislation Considered Harmful" sounds exciting right? That is from the national news. It looks like the "Go To" title started a bit of a theme in CS articles: "Global variables considered harmful" and "The letter O considered harmful."
Global variables I can see, and wish that the message got out more on that, but really, the letter O? (Let me tell you it was a real problem, 0 and O didn't always look so different. In fact my typewriter doesn't even have a zero, gotta use the O to make a zero so yeah the problem is real. See also l and 1 bet you have to squint to figure out which is 'ell' and which is 'one', 1 would tell you but 1 already forgot. (Are we sure computers use Ones and Zeros or do they use Ohs and Ells?))
If Dijkstra's title is where you stopped reading, you better try again... wait if you stopped at Dijkstra's title how did you get this far in this silly little blog post? Doesn't matter! Go read the rest of Dijkstra's article it is only a few pages:
https://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF
Hmm, interesting. That was published in 1968 in case you missed that detail. They sure knew a lot back then.
What I got from it is the article is that Goto causes serious issues with being able to tell how we got to where we are in a program. We call a stack trace now but only because we can walk the stack and know there wasn't any funny business.
My little trick above is a bit of an illusion. When you start allowing stuff like that people become a little immune to goto and will let it slip in other places, then pandemonium.
I was wondering what it would be like if the paper had retained it's original title. Would it have gained traction and would goto have been effectively banished as it is today? We may never know unless we were able to shift into the parallel universe where it was not banned.
Well, I traveled to that parallel universe. There are companies that run custom home grown software written in BASIC. Yes, that BASIC:
10 print "hello world"
20 goto 10
Yes, THAT BASIC. I took on some work there and let me tell you it sure was interesting. On one hand BASIC grew in the last many years since I learned it first and the flavor they are using has some features that effectively allow you to not use that basic. The client didn't get the message. So yes, if the title had remained "A case against the goto statement.", we might all have been doomed to live in this weird parallel universe.
Who was the masked stranger that saved us all from certain doom? No mask and not a stranger it is Niklaus Wirth. (Europeans call him by name, Americans by value (his joke, not mine))
If you have not heard of him, please look him up. He was instrumental in creating Pascal, Modula, Oberon programming languages. Also EBNF can be traced back to him along with "Railroad Diagrams" (aka Syntax Diagrams)
Is Goto harmful?
Maybe, maybe not. Did Dijkstra formulate a case against it, yes I think definitely. Goto is going to be there at a low level but hidden away by tools of a more civilised age.
Is Tail Recursion harmful?
I think that is the wrong question. I think the real question is: Is Tail Recursion the same as using a Goto? Which I think the answer is no. Earlier today I thought maybe they were the same, after thinking and reading Dijkstra's article again which calls recursion in place of goto I am more convinced that goto can go.
Gotos replacing tail recursion are a gateway to mass hysteria. Gotos everywhere, jumping into the middle of a function with no way to know how you got there or where you came from. Sounds like I am exaggerating, maybe I am, but not by much (been there and done that it's not pretty.)
Links to more on this:
https://en.wikipedia.org/wiki/Considered_harmful
https://en.wikipedia.org/wiki/Niklaus_Wirth
https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form
https://en.wikipedia.org/wiki/Syntax_diagram
What I find most interesting about this is the link between Dijkstra and Wirth, not something I usually think about. Nobody works in a vacuum and this is one instance that highlights the collaboration in an interesting way.
It really is fun looking into some of the history behind what we do. With the history so recent I wonder why it is not purposefully studied more.
One last thing:
I had trouble deciding on the title for this the alternate is
"Considered Harmful" Considered Harmful ... see what I did there? A little recursion in the title. Let me know in the comments which title you would have picked.
You can also read about this here: Tail Recursion considered harmful?
Wednesday, January 22, 2014
GR Testers lightning talk notes
Notes from my GR Testers lightning talk:
Tuckman's stages of group development.
- Form
- The team forms and comes together initially
- Initial period of politeness while forming
- "Honeymoon" period
- Storm
- Personalities come out
- Conflict arise over differing ideals
- Roles within the teams are being sorted out
- Norm
- A mutual plan converges and forms
- Relaxing of individual ideals
- Perform
- Able to get a job done smoothly and efficiently
- Communication barriers a reduced
- Stages
- Not all teams get to each step
- Sometimes there is reversion to earlier stages
- Later an "Adjourning" stage was added, but it does not rhyme so we don't say it.
- Team manager role adjusts greatly through the stages of development
- One of the common by-products is one of the team members is ready to become a team manager the team and the old manager will restart the process with a new team. Some managers fear this, but it is a natural and key part of creating high performing teams.
- Links:
- http://www.armyg1.army.mil/eo/docs/group_dev_tng.pdf
- http://en.wikipedia.org/wiki/Tuckman's_stages_of_group_development
- http://changingminds.org/explanations/groups/form_storm_norm_perform.htm
- http://www.myteamtype.com/uncategorized/form-storm-norm-the-5-stages-of-group-development/
- http://www.businessballs.com/tuckmanformingstormingnormingperforming.htm
Friday, October 25, 2013
Code and Coffee in Grand Rapids, Michigan
Coming soon to Grand Rapids: "Code & Coffee"
What is it? Just something fun to do in the morning before real work kicks in. Get together and write some code, pair up, or just chat and solve problems. For more information see: http://codeandcoffee.info/
The first round of Code & Coffee will be on Thursday October 31, 2103 at 7:00 am at "Tim Hortons" on Fulton.
Tim Hortons900 Fulton St, West
Grand Rapids, MI 49504
Directions
Hope to see you there.
What is it? Just something fun to do in the morning before real work kicks in. Get together and write some code, pair up, or just chat and solve problems. For more information see: http://codeandcoffee.info/
The first round of Code & Coffee will be on Thursday October 31, 2103 at 7:00 am at "Tim Hortons" on Fulton.
Tim Hortons900 Fulton St, West
Grand Rapids, MI 49504
Directions
Hope to see you there.
Tuesday, November 20, 2012
Kata battles: Toolset edition
I was fiddling with the Coin Changer Kata, an exercise meant to build skill through repetition. It turns out that a Code Kata is also an interesting way to compare toolsets. I was working in two very different languages with different environments. We will give the languages a little anonymity in order to delay judgement.
The first language I tried is a runtime interpreted language, scripting language if you will. The environment I used was very light: Vim, a simple testing framework and the interpreter. It took a little time to get setup and become accustom to the testing style but not long. The biggest problems I had were with unfamiliarity with the language with checking a list of value. The red, green, refactor cycle was smooth and fast. The tests would run super fast so I could write a test, watch it fail, make it pass faster than I can. The environment was not the bottleneck. A good setup for increasing skill.
The second language is a compile it then run it language. Compiled programs are faster right? Not from what I experienced last night. The environment is much heavier. Full IDE, everything is build in. A simple key combination and the whole project will build. Automatic completion of nearly everything. Really a cool system, after you download and install all it. And pay for it if you want even more features. And pay more for it if you want EVEN MORE features. So I get the IDE up and runnning. Start a new project. Start a test project. Realize I have no test framework. Install the test framework we use at work and get going. And get stuck. I can no longer build because the test runner has files open that the IDE needs to overwrite. At this point I decide I need the refactoring tool we have at work. The refactoring tool comes with a test runner that I like a lot better, but the at home budget dictates that the tool will have to wait. So I do what we all do when we get stuck. Google said I need to enable a service on the host computer. Ok, still does not work. Restart test runner, no go. Restart IDE and we are in business. Write a test............red. Make it pass......... green. Not time to refactor yet, there is barely any code. Just enough code to make the test pass in fact. And good thing we don't have to refactor it takes too long to run the tests. A few more red and green cycles and I have run out of time. It take FOREVER to run the tests, and I don't even have to do anything to run them they automatically kick off each time I build. This is not TDD at its best. This is not fun. This is not over. I know I can get a better setup. It will just take a little tweaking and fiddling. I am willing to do that because I know there is a better way.
How many people have tried this TDD thing and become frustrated with a lousy environment? How many people have been totally turned off to TDD because of issues like this?
Anyway stay tuned to find out how I improve the environment. I will be benchmarking the before and after times for a whole Kata and also test run times. I hope to end up with an environment that will let me focus on improving skill rather than de-focus and wander off while my tests are trying to run.
The first language I tried is a runtime interpreted language, scripting language if you will. The environment I used was very light: Vim, a simple testing framework and the interpreter. It took a little time to get setup and become accustom to the testing style but not long. The biggest problems I had were with unfamiliarity with the language with checking a list of value. The red, green, refactor cycle was smooth and fast. The tests would run super fast so I could write a test, watch it fail, make it pass faster than I can. The environment was not the bottleneck. A good setup for increasing skill.
The second language is a compile it then run it language. Compiled programs are faster right? Not from what I experienced last night. The environment is much heavier. Full IDE, everything is build in. A simple key combination and the whole project will build. Automatic completion of nearly everything. Really a cool system, after you download and install all it. And pay for it if you want even more features. And pay more for it if you want EVEN MORE features. So I get the IDE up and runnning. Start a new project. Start a test project. Realize I have no test framework. Install the test framework we use at work and get going. And get stuck. I can no longer build because the test runner has files open that the IDE needs to overwrite. At this point I decide I need the refactoring tool we have at work. The refactoring tool comes with a test runner that I like a lot better, but the at home budget dictates that the tool will have to wait. So I do what we all do when we get stuck. Google said I need to enable a service on the host computer. Ok, still does not work. Restart test runner, no go. Restart IDE and we are in business. Write a test............red. Make it pass......... green. Not time to refactor yet, there is barely any code. Just enough code to make the test pass in fact. And good thing we don't have to refactor it takes too long to run the tests. A few more red and green cycles and I have run out of time. It take FOREVER to run the tests, and I don't even have to do anything to run them they automatically kick off each time I build. This is not TDD at its best. This is not fun. This is not over. I know I can get a better setup. It will just take a little tweaking and fiddling. I am willing to do that because I know there is a better way.
How many people have tried this TDD thing and become frustrated with a lousy environment? How many people have been totally turned off to TDD because of issues like this?
Anyway stay tuned to find out how I improve the environment. I will be benchmarking the before and after times for a whole Kata and also test run times. I hope to end up with an environment that will let me focus on improving skill rather than de-focus and wander off while my tests are trying to run.
Subscribe to:
Posts (Atom)
