Tree datastructures: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: Tidied one function.) |
|||
Line 705: | Line 705: | ||
0 < xs.length ? (() => { |
0 < xs.length ? (() => { |
||
const [n, s] = Array.from(xs[0]); |
const [n, s] = Array.from(xs[0]); |
||
return uncurry(cons)( |
|||
splitArrow(compose(Node(s), go))(go)( |
|||
span(compose(lt(n), fst))( |
|||
xs.slice(1) |
|||
⚫ | |||
) |
|||
); |
); |
||
// This first tree, and then the rest. |
|||
⚫ | |||
⚫ | |||
})() : []; |
})() : []; |
||
return go(tuples); |
return go(tuples); |
||
Line 770: | Line 769: | ||
const compose = (...fs) => |
const compose = (...fs) => |
||
x => fs.reduceRight((a, f) => f(a), x); |
x => fs.reduceRight((a, f) => f(a), x); |
||
// cons :: a -> [a] -> [a] |
|||
const cons = x => |
|||
xs => [x].concat(xs) |
|||
// div :: Int -> Int -> Int |
// div :: Int -> Int -> Int |
||
Line 825: | Line 828: | ||
// lines :: String -> [String] |
// lines :: String -> [String] |
||
const lines = s => s.split(/[\r\n]/); |
const lines = s => s.split(/[\r\n]/); |
||
// lt (<) :: Ord a => a -> a -> Bool |
|||
const lt = a => b => a < b; |
|||
// minimum :: Ord a => [a] -> a |
// minimum :: Ord a => [a] -> a |
||
Line 831: | Line 837: | ||
foldl1(a => x => x < a ? x : a)(xs) |
foldl1(a => x => x < a ? x : a)(xs) |
||
) : undefined; |
) : undefined; |
||
// showLog :: a -> IO () |
|||
const showLog = (...args) => |
|||
console.log( |
|||
args |
|||
.map(JSON.stringify) |
|||
.join(' -> ') |
|||
); |
|||
// span :: (a -> Bool) -> [a] -> ([a], [a]) |
// span :: (a -> Bool) -> [a] -> ([a], [a]) |
||
Line 841: | Line 855: | ||
)(xs); |
)(xs); |
||
}; |
}; |
||
// Compose a function (from a tuple to a tuple), |
|||
// (with separate transformations for fst and snd) |
|||
// splitArrow (***) :: (a -> b) -> (c -> d) -> ((a, c) -> (b, d)) |
|||
const splitArrow = f => g => |
|||
tpl => Tuple(f(tpl[0]))( |
|||
g(tpl[1]) |
|||
); |
|||
// splitAt :: Int -> [a] -> ([a], [a]) |
// splitAt :: Int -> [a] -> ([a], [a]) |
||
Line 853: | Line 876: | ||
// uncurry :: (a -> b -> c) -> ((a, b) -> c) |
// uncurry :: (a -> b -> c) -> ((a, b) -> c) |
||
const uncurry = f => |
const uncurry = f => |
||
( |
function() { |
||
const |
|||
args = Array.from(arguments), |
|||
a = 1 < args.length ? ( |
|||
args |
|||
) : args[0]; |
|||
⚫ | |||
}; |
|||
// until :: (a -> Bool) -> (a -> a) -> a -> a |
// until :: (a -> Bool) -> (a -> a) -> a -> a |