Created
March 19, 2014 17:56
-
-
Save threeifbywhiskey/9647510 to your computer and use it in GitHub Desktop.
This is a solution to /r/dailyprogrammer's Intermediate challenge #154 written almost entirely with numbers and lambdas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MAP = -> coll, &fn { | |
coll == [] ? [] : [fn[coll[0]]] + MAP[coll[1..-1], &fn] | |
} | |
SIZE = -> coll { | |
coll == [] || coll == '' ? 0 : 1 + SIZE[coll[1..-1]] | |
} | |
FILTER = -> coll, &fn { | |
coll == [] ? [] : (fn[coll[0]] ? [coll[0]] : []) + FILTER[coll[1..-1], &fn] | |
} | |
SORT_BY = -> coll, &fn { | |
x, *xs = *coll | |
coll == [] ? [] | |
: SORT_BY[FILTER[xs, &-> y { (fn[x] <=> fn[y]) == 1 }], &fn] + | |
[x] + | |
SORT_BY[FILTER[xs, &-> y { (fn[x] <=> fn[y]) <= 0 }], &fn] | |
} | |
ORD = -> c { | |
(order = -> i { | |
'' << i == c ? i : order[i + 1] | |
})[1] | |
} | |
CHARS = -> str { | |
sz = SIZE[str] | |
(chars_p = -> ret, i { | |
ret << str[i] | |
(i += 1) == sz ? ret : chars_p[ret, i] | |
})[[], 0] | |
} | |
INDEX = -> coll, elem { | |
(indexer = -> i { | |
coll[i] == elem ? i : i > SIZE[coll] ? nil : indexer[i + 1] | |
})[0] | |
} | |
EACH = -> coll, &fn { | |
sz = SIZE[coll] | |
(eacher = -> i { | |
fn[coll[i]] | |
(i += 1) == sz ? coll : eacher[i] | |
})[0] | |
} | |
PUTS = -> obj { $> << obj << "\n" } | |
GORELLIAN = -> n, alphabet, words { | |
alphabet = MAP[CHARS[alphabet], &-> a { ORD[a] | 32 }] | |
EACH[SORT_BY[words, &-> w {MAP[CHARS[w], | |
&-> c { INDEX[alphabet, ORD[c] | 32] }] }], &PUTS] | |
} | |
GORELLIAN[8, 'UVWXYZNOPQRSTHIJKLMABCDEFG', | |
['ANTLER', 'ANY', 'COW', 'HILL', 'HOW', 'HOWEVER', 'WHATEVER', 'ZONE']] | |
PUTS[''] | |
GORELLIAN[5, 'ZYXWVuTSRQpONMLkJIHGFEDCBa', | |
['go', 'aLL', 'ACM', 'teamS', 'Go']] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment