Tree datastructures: Difference between revisions

m
m (→‎{{header|Haskell}}: Tidied one function.)
Line 705:
0 < xs.length ? (() => {
const [n, s] = Array.from(xs[0]);
//return Lines indented under this line,uncurry(cons)(
// tupled with all thesplitArrow(compose(Node(s), rest.go))(go)(
const [firstTreeLines, rest] = Array.from span(compose(lt(n), fst))(
span(x => n < x[0])( xs.slice(1))
.concat(go(rest ));
)
);
// This first tree, and then the rest.
return [Node(s)(go(firstTreeLines))]
.concat(go(rest));
})() : [];
return go(tuples);
Line 770 ⟶ 769:
const compose = (...fs) =>
x => fs.reduceRight((a, f) => f(a), x);
 
// cons :: a -> [a] -> [a]
const cons = x =>
xs => [x].concat(xs)
 
// div :: Int -> Int -> Int
Line 825 ⟶ 828:
// lines :: String -> [String]
const lines = s => s.split(/[\r\n]/);
 
// lt (<) :: Ord a => a -> a -> Bool
const lt = a => b => a < b;
 
// minimum :: Ord a => [a] -> a
Line 831 ⟶ 837:
foldl1(a => x => x < a ? x : a)(xs)
) : undefined;
 
// showLog :: a -> IO ()
const showLog = (...args) =>
console.log(
args
.map(JSON.stringify)
.join(' -> ')
);
 
// span :: (a -> Bool) -> [a] -> ([a], [a])
Line 841 ⟶ 855:
)(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])
Line 853 ⟶ 876:
// uncurry :: (a -> b -> c) -> ((a, b) -> c)
const uncurry = f =>
function(x, y) => f(x)(y);{
const
args = Array.from(arguments),
a = 1 < args.length ? (
args
) : args[0];
return f(a[Node(s0])(go(firstTreeLinesa[1]))];
};
 
// until :: (a -> Bool) -> (a -> a) -> a -> a
9,659

edits