0
map
is a very basic yet important function in functional programming.
All FP programming languages have it built-in but it is always fun to see how shortly you can define it yourself.
This will be scored in AST nodes, not bytes.
A token is the second-smallest building block of a program, over characters.
A token is:
anything that would be a distinct node if you drew a tree representing the source code
This scoring gives no benefit to single letter AST nodes names, and values succinctness as Paul Graham likes (this definition of AST nodes is his).
Haskell has this super cool definition:
zipWith ($) . repeat
And my lisp-ish AST of it (not required in your answer, just to show it visually)
Apply (Apply (Var ".") (Apply (Var "zipWith") (Var "$"))) (Var "repeat")
That is 7 tokens. (Var x
counts as 1)
I did not write it, so I do not post it as an answer.
Example:
(zipWith ($) . repeat) (* 3) [1, 2, 3] -- [3, 6, 9]
map
should be defined as a (named or unnamed) function taking two arguments, the function and the list.
May the most AST nodes-succint code win!
5
exec "blah blah blah"
<- 2 tokens, right? – feersum – 2015-11-23T18:31:00.147@feersum nice loophole, but if you write code in a string, than you should count the tokens inside the string. Otherwise everything would be written in 2 tokens :) – Caridorc – 2015-11-23T18:32:17.430
@feersum or I may disable exec in general as it is just a cheap way to circle around the rules... – Caridorc – 2015-11-23T18:36:32.463
Should the map be in-place, or should it return a new array? – Doorknob – 2015-11-23T18:36:37.100
@Doorknob mutability is not allowed in FP. Return a new array – Caridorc – 2015-11-23T18:37:01.517
3I'm voting to close as unclear because tokens are not precisely defined. – feersum – 2015-11-23T18:38:07.930
How should the map be implemented? As a function with two arguments, an array and another function? Or can it just assume that the variable names of the array and the function will be constant? – Doorknob – 2015-11-23T18:38:52.607
@feersum true, I should have used the sandbox... I will try my best to define a token – Caridorc – 2015-11-23T18:39:26.523
@Doorknob function with two arguments – Caridorc – 2015-11-23T18:40:00.547
@feersum defined a token – Caridorc – 2015-11-23T18:46:12.867
1I don't think your definition really clears things up. If I drew an AST of your source code, it would have "apply" nodes which don't correspond to tokens of the source. – Peter Taylor – 2015-11-23T19:03:56.940
@PeterTaylor i am not good at doing AST so probably your tree is correct and mine is wrong. – Caridorc – 2015-11-23T19:04:56.767
@PeterTaylor in fact I suspec that [Space] should also be a token – Caridorc – 2015-11-23T19:05:23.797
@PeterTaylor wonderful people on #haskell freenode gave me good info on AST – Caridorc – 2015-11-23T19:34:06.010
@PeterTaylor The correct tree is:
Apply (Apply (Var ".") (Apply (Var "zipWith") (Var "$"))) (Var "repeat")
whereVar x
counts as 1 – Caridorc – 2015-11-23T19:34:43.6833I'd love to see a meta post about this "tokens" concept, to clear this all up before it's used in challenges. – Sparr – 2015-11-23T20:29:50.033
@Sparr I think just saying AST nodes instead of tokens should clear it up – Caridorc – 2015-11-23T20:36:36.863
I chose to define
map
asmap
. Bam, 1 AST node. ;) – Morgan Thrapp – 2015-11-23T21:53:33.4571@MorganThrapp maybe, just maybe if a challenge is implementing x, saying x = builtin_x is a loophole ;) – Caridorc – 2015-11-23T21:55:22.437
2@Caridorc Define a "builtin map". For example, would the Python list comprehension answer be valid? – LegionMammal978 – 2015-11-23T22:45:05.990
@LegionMammal978 I would think so. It does the full workload of creating a new list, calling the transformation function for each item in the given list, and appending each resulting item to the new list. What more could you want from a self-contained
map()
implementation? – Blacklight Shining – 2015-11-24T05:13:09.253@BlacklightShining The thing is, thee user doesn't do that, Python does it. – LegionMammal978 – 2015-11-24T11:54:31.053
@LegionMammal978: In the same way that Python creates a list when
foo_list = []
is executed? – Blacklight Shining – 2015-11-27T18:03:01.637@BlacklightShining In that case, the user "does the full workload of" defining the 0 elements of
[]
. – LegionMammal978 – 2015-11-27T18:34:56.193@LegionMammal978 How do we decide where the line is, then? I'm inclined to accept the list-comprehension answer, given that it's syntactic sugar for creating a new list, etc, etc. – Blacklight Shining – 2015-11-30T03:43:52.733
1@BlacklightShining Let the OP decide. – LegionMammal978 – 2015-11-30T11:25:34.720