raganwald
Saturday, November 24, 2007
  How I've been spending my time off work lately
The weather being fine, and in the flagrant pursuit of happiness, I’ve managed to get a few holes in this week (using Ruby of course). Here’s my scorecard:

palindrome = (+(+[] | +[-:_] | ((-'[0]' >> -:ends) & (-'[1..-2]' >> -:p)
& (-'[-1]' >> -:ends) & -:_)) >> (-:'{}' >> -:p)).call
(palindrome =~ []).should_not be_nil
(palindrome =~ [:fish]).should_not be_nil
(palindrome =~ [:fish, :dog]).should be_nil
(palindrome =~ [:fish, :dog, :fish]).should_not be_nil
(palindrome =~ [:fish, :fish, :fish, :fish]).should_not be_nil
(palindrome =~ [:fish, :fish, :fish, :fish, :fish]).should_not be_nil
(palindrome =~ [:fish, :dog, :dog, :fish]).should_not be_nil
(palindrome =~ [:fish, :dog, :cat, :dog, :fish]).should_not be_nil
(palindrome =~ [:fish, :dog, :cat, :fish]).should be_nil

Perhaps Guido has a point, indentation matters:

palindrome = (
+( # we'll start by creating a pattern
+[] | # match the empty list, or:
+[-:_] | # match a list containing anything
# (-:_ matches any one entity), or:
( # alternative three:
(-'[0]' >> -:ends) & # extract the first element and
# bind it to 'ends'
(-'[1..-2]' >> -:p) & # and everything from the second to the
# second-last will be matched against a
# pattern :p
(-'[-1]' >> -:ends) & # and the last element must match whatever
# was bound to 'ends' above.
# it's equally effective to include
# -'_[0].eql?(_[1])', same thing.
-:_ # if all that matches, match the entire array
# so that the result is the entire array, not
# whatever part of the array we were just
# matching
) # therein ends the pattern
) >> (-:'{}' >> -:p) # pass the pattern to a closure-maker (-:'{}')
# and then bind it to 'p' so that the pattern
# is now recursive: this pattern matches an
# empty list, a list with one element, or
# a list with the first and last elements eql?
# to each other and whatever is in-between them
# matching the palindrome pattern bound to 'p'.
).call

Hmmm. Still doesn’t make any sense. I guess home-brew programs are like babies: they appear to be delightful and impart a sense of wonder in the mysteries of the universe to their creators, but are noisy and obstreperous to everyone else.

f = (
+(
(-'==0' >> -'0') |
(-'==1' >> -'1') |
(-'n>1' >> -'n * f[n-1]') ) >> (-:'{}' >> -:f)).call
f[0].should == 0
f[1].should == 1
f[2].should == 2
f[3].should == 6
f[4].should == 24
f[5].should == 120

Labels:

 


Thank you for reading my work. Did you enjoy this post? You can find more like it in the sidebar. Please browse the archives, there are lots of hidden gems. A Brief History of Dangerous Ideas explains what I am up to at the moment.
Comments on “How I've been spending my time off work lately:
Hm. I get the following when I try it:

SyntaxError: compile error
(irb):2: syntax error, unexpected tAMPER, expecting ')'
& (-'[-1]' >> -:ends) & -:_)) >> (-:'{}' >> -:p)).call
^

Are you forgetting to tell us something? :)
 
I must have omitted a require ;-)
 
Wait, fibs and facts are not the same! :-)
 
Matt, now you know why I am not big on using "FizBuzz" interviews. I would probably mix up the fizzing and buzzing!
 
Post a Comment




<< Home
Reg Braithwaite


Nota Bene
A Brief History of Dangerous Ideas

Share
rewrite.rubyforge.org / ick.rubyforge.org / andand.rubyforge.org / unfold.rb / string_to_proc.rb / dsl_and_let.rb / comprehension.rb / lazy_lists.rb

Beauty
IS-STRICTLY-EQUIVALENT-TO-A / Spaghetti-Western Coding / Golf is a good program spoiled / Programming conventions as signals / Not all functions should be object methods

The Not So Big Software Design / Writing programs for people to read / Why Why Functional Programming Matters Matters / But Y would I want to do a thing like this?

Work
The single most important thing you must do to improve your programming career / The Naïve Approach to Hiring People / No Disrespect / Take control of your interview / Three tips for getting a job through a recruiter / My favourite interview question

Jobs

Management
Exception Handling in Software Development / What if powerful languages and idioms only work for small teams? / Bricks / Which theory fits the evidence? / Still failing, still learning / What I’ve learned from failure

Notation
The unary ampersand in Ruby / (1..100).inject(&:+) / The challenge of teaching yourself a programming language / The significance of the meta-circular interpreter / Block-Structured Javascript / Haskell, Ruby and Infinity / Closures and Higher-Order Functions

Opinion
Why Apple is more expensive than Amazon / Why we are the biggest obstacles to our own growth / Is software the documentation of business process mistakes? / We have lost control of the apparatus / What I’ve Learned From Sales I, II, III

Whimsey
The Narcissism of Small Code Differences / Billy Martin’s Technique for Managing his Manager / Three stories about The Tao / Programming Language Stories / Why You Need a Degree to Work For BigCo