Skip to content

Instantly share code, notes, and snippets.

@bzar
Last active August 24, 2016 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bzar/13c0bef5d65f42cc874269b4117db4d1 to your computer and use it in GitHub Desktop.
Save bzar/13c0bef5d65f42cc874269b4117db4d1 to your computer and use it in GitHub Desktop.
{reverse, sum, empty, all, fold, concat-map, flatten, span, Obj, sort-by, head} = require 'prelude-ls'
# [[String]] -> Bool
in-order = (xss) -> all (-> it.length == 1), xss
# [[String] -> Data -> [[String]]] -> Data -> [String] -> [[String]]
order-by-one = (fs, d, xs) -->
| xs.length < 2 => [xs]
| otherwise =>
fold ((ys, f) -> if ys.length == 1 then f(ys[0], d) else ys), [xs], fs
# [[String] -> Data -> [[String]]] -> [[String]] -> Data -> [[String]]
order-by-many = (fs, xss, d) ->
let yss = concat-map order-by-one(fs, d), xss
switch
| yss.length == xss.length => flatten yss
| in-order yss => flatten yss
| otherwise => order-by-many fs, yss, d
# [[String] -> Data -> [[String]]] -> [String] -> Data -> [String]
multisort = (fs, xs, d) -> order-by-many fs, [xs], d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment