I was trying to learn me some F# by looking at last years AdventOfCode solutions. I came across this neat peice of code, which I cannot parse at all:
i 1|>Seq.sumBy(" (".IndexOf)
Note, I believe I understand the prior line (in the link):
let i n=System.IO.File.ReadAllText(sprintf "%s/input/input%d.txt"__SOURCE_DIRECTORY__ n)
Which creates a function i
that takes an integer n
and reads the file inputN.txt and returns it as a string. Therefore i 1
returns input1.txt
as a string.
Then |>
is just piping the string (or array of chars?) as the first param to the next function, which is Seq.sumBy
But then things start breaking down...
sumBy seems straight forward enough:
Returns the sum of the results generated by applying the function to each element of the list.
But the IndexOf
of a string " ("
has me baffled.
Now, I don't really want any fishes here, what I would like to know is this. As a newbie to this foreign language, as I learn to work more bits of F#, how can I take this piece of code and decompose it into smaller pieces to test it to figure out what is going on? It is driving me nuts that I have the solution, have google/so, and I still can't understand this code.
Can someone show me some smaller snippets so I can discover the answer myself?