diff --git a/MD applescript vs javascript/Just.md b/MD applescript vs javascript/Just.md index 137ff8d8..e189f6f4 100644 --- a/MD applescript vs javascript/Just.md +++ b/MD applescript vs javascript/Just.md @@ -1,13 +1,3 @@ -```javascript -// Just :: a -> Maybe a -const Just = x => ({ - type: 'Maybe', - Nothing: false, - Just: x -}); -``` - - ```applescript -- Just :: a -> Maybe a on Just(x) @@ -15,4 +5,14 @@ on Just(x) -- Wrapper containing the result of a computation. {type:"Maybe", Nothing:false, Just:x} end Just +``` + + +```javascript +// Just :: a -> Maybe a +const Just = x => ({ + type: 'Maybe', + Nothing: false, + Just: x +}); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/Left.md b/MD applescript vs javascript/Left.md index dc3447cd..761c2424 100644 --- a/MD applescript vs javascript/Left.md +++ b/MD applescript vs javascript/Left.md @@ -1,15 +1,15 @@ +```applescript +-- Left :: a -> Either a b +on |Left|(x) + {type:"Either", |Left|:x, |Right|:missing value} +end |Left| +``` + + ```javascript // Left :: a -> Either a b const Left = x => ({ type: 'Either', Left: x }); -``` - - -```applescript --- Left :: a -> Either a b -on |Left|(x) - {type:"Either", |Left|:x, |Right|:missing value} -end |Left| ``` \ No newline at end of file diff --git a/MD applescript vs javascript/Node.md b/MD applescript vs javascript/Node.md index cd2405cf..2abdf6a4 100644 --- a/MD applescript vs javascript/Node.md +++ b/MD applescript vs javascript/Node.md @@ -1,3 +1,11 @@ +```applescript +-- Node :: a -> [Tree a] -> Tree a +on Node(v, xs) + {type:"Node", root:v, nest:xs} +end Node +``` + + ```javascript // Node :: a -> [Tree a] -> Tree a const Node = v => @@ -9,12 +17,4 @@ const Node = v => root: v, nest: xs || [] }); -``` - - -```applescript --- Node :: a -> [Tree a] -> Tree a -on Node(v, xs) - {type:"Node", root:v, nest:xs} -end Node ``` \ No newline at end of file diff --git a/MD applescript vs javascript/Nothing.md b/MD applescript vs javascript/Nothing.md index dc275b1b..dff65085 100644 --- a/MD applescript vs javascript/Nothing.md +++ b/MD applescript vs javascript/Nothing.md @@ -1,12 +1,3 @@ -```javascript -// Nothing :: Maybe a -const Nothing = () => ({ - type: 'Maybe', - Nothing: true, -}); -``` - - ```applescript -- Nothing :: Maybe a on Nothing() @@ -14,4 +5,13 @@ on Nothing() -- Empty wrapper returned where a computation is not possible. {type: "Maybe", Nothing: true} end Nothing +``` + + +```javascript +// Nothing :: Maybe a +const Nothing = () => ({ + type: 'Maybe', + Nothing: true, +}); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/Right.md b/MD applescript vs javascript/Right.md index 17cb53f3..b55bcd2c 100644 --- a/MD applescript vs javascript/Right.md +++ b/MD applescript vs javascript/Right.md @@ -1,15 +1,15 @@ +```applescript +-- Right :: b -> Either a b +on |Right|(x) + {type:"Either", |Left|:missing value, |Right|:x} +end |Right| +``` + + ```javascript // Right :: b -> Either a b const Right = x => ({ type: 'Either', Right: x }); -``` - - -```applescript --- Right :: b -> Either a b -on |Right|(x) - {type:"Either", |Left|:missing value, |Right|:x} -end |Right| ``` \ No newline at end of file diff --git a/MD applescript vs javascript/Tuple.md b/MD applescript vs javascript/Tuple.md index 3ad64cdd..4ff58701 100644 --- a/MD applescript vs javascript/Tuple.md +++ b/MD applescript vs javascript/Tuple.md @@ -1,3 +1,12 @@ +```applescript +-- Tuple (,) :: a -> b -> (a, b) +on Tuple(a, b) + -- Constructor for a pair of values, possibly of two different types. + {type:"Tuple", |1|:a, |2|:b, length:2} +end Tuple +``` + + ```javascript // Tuple (,) :: a -> b -> (a, b) const Tuple = a => @@ -7,13 +16,4 @@ const Tuple = a => '1': b, length: 2 }); -``` - - -```applescript --- Tuple (,) :: a -> b -> (a, b) -on Tuple(a, b) - -- Constructor for a pair of values, possibly of two different types. - {type:"Tuple", |1|:a, |2|:b, length:2} -end Tuple ``` \ No newline at end of file diff --git a/MD applescript vs javascript/Tuple3.md b/MD applescript vs javascript/Tuple3.md index 262b6738..f80341dd 100644 --- a/MD applescript vs javascript/Tuple3.md +++ b/MD applescript vs javascript/Tuple3.md @@ -1,3 +1,11 @@ +```applescript +-- Tuple3 (,,) :: a -> b -> c -> (a, b, c) +on Tuple3(x, y, z) + {type:"Tuple3", |1|:x, |2|:y, |3|:z, length:3} +end Tuple3 +``` + + ```javascript // Tuple3 (,,) :: a -> b -> c -> (a, b, c) const Tuple3 = a => b => c => ({ @@ -7,12 +15,4 @@ const Tuple3 = a => b => c => ({ '2': c, length: 3 }); -``` - - -```applescript --- Tuple3 (,,) :: a -> b -> c -> (a, b, c) -on Tuple3(x, y, z) - {type:"Tuple3", |1|:x, |2|:y, |3|:z, length:3} -end Tuple3 ``` \ No newline at end of file diff --git a/MD applescript vs javascript/TupleN.md b/MD applescript vs javascript/TupleN.md index da7494b4..b05c5be8 100644 --- a/MD applescript vs javascript/TupleN.md +++ b/MD applescript vs javascript/TupleN.md @@ -1,3 +1,13 @@ +```applescript +-- Requires N arguments to be wrapped as one list in AS +-- (the JS version accepts N separate arguments) +-- TupleN :: a -> b ... -> (a, b ... ) +on TupleN(argv) + tupleFromList(argv) +end TupleN +``` + + ```javascript // TupleN :: a -> b ... -> (a, b ... ) function TupleN() { @@ -13,14 +23,4 @@ function TupleN() { }) ) : args.reduce((f, x) => f(x), Tuple); } -``` - - -```applescript --- Requires N arguments to be wrapped as one list in AS --- (the JS version accepts N separate arguments) --- TupleN :: a -> b ... -> (a, b ... ) -on TupleN(argv) - tupleFromList(argv) -end TupleN ``` \ No newline at end of file diff --git a/MD applescript vs javascript/abs.md b/MD applescript vs javascript/abs.md index 1d7bb4be..4f49f408 100644 --- a/MD applescript vs javascript/abs.md +++ b/MD applescript vs javascript/abs.md @@ -1,11 +1,3 @@ -```javascript -// abs :: Num -> Num -const abs = -// Absolute value of a given number - without the sign. - Math.abs; -``` - - ```applescript -- abs :: Num -> Num on abs(x) @@ -16,4 +8,12 @@ on abs(x) x end if end abs +``` + + +```javascript +// abs :: Num -> Num +const abs = +// Absolute value of a given number - without the sign. + Math.abs; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/add.md b/MD applescript vs javascript/add.md index eb241dba..3cacc6ea 100644 --- a/MD applescript vs javascript/add.md +++ b/MD applescript vs javascript/add.md @@ -1,11 +1,3 @@ -```javascript -// add (+) :: Num a => a -> a -> a -const add = a => - // Curried addition. - b => a + b; -``` - - ```applescript -- add (+) :: Num a => a -> a -> a on add(a) @@ -16,4 +8,12 @@ on add(a) end |λ| end script end add +``` + + +```javascript +// add (+) :: Num a => a -> a -> a +const add = a => + // Curried addition. + b => a + b; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/all.md b/MD applescript vs javascript/all.md index d2dcb60a..f4dc0767 100644 --- a/MD applescript vs javascript/all.md +++ b/MD applescript vs javascript/all.md @@ -1,11 +1,3 @@ -```javascript -// all :: (a -> Bool) -> [a] -> Bool -const all = p => - // True if p(x) holds for every x in xs. - xs => [...xs].every(p); -``` - - ```applescript -- all :: (a -> Bool) -> [a] -> Bool on all(p, xs) @@ -18,4 +10,12 @@ on all(p, xs) true end tell end all +``` + + +```javascript +// all :: (a -> Bool) -> [a] -> Bool +const all = p => + // True if p(x) holds for every x in xs. + xs => [...xs].every(p); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/allSame.md b/MD applescript vs javascript/allSame.md index 1e851ea9..a4b3f32b 100644 --- a/MD applescript vs javascript/allSame.md +++ b/MD applescript vs javascript/allSame.md @@ -1,12 +1,3 @@ -```javascript -// allSame :: [a] -> Bool -const allSame = xs => - 2 > xs.length || ( - h => xs.slice(1).every(x => h === x) - )(xs[0]); -``` - - ```applescript -- allSame :: [a] -> Bool on allSame(xs) @@ -22,4 +13,13 @@ on allSame(xs) all(p, rest of xs) end if end allSame +``` + + +```javascript +// allSame :: [a] -> Bool +const allSame = xs => + 2 > xs.length || ( + h => xs.slice(1).every(x => h === x) + )(xs[0]); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/allTree.md b/MD applescript vs javascript/allTree.md index c32ca6b8..a58e2eb9 100644 --- a/MD applescript vs javascript/allTree.md +++ b/MD applescript vs javascript/allTree.md @@ -1,14 +1,3 @@ -```javascript -// allTree :: (a -> Bool) -> Tree a -> Bool -const allTree = p => - // True if p holds for all nodes of the - // tree to which allTree(p) is applied. - foldTree( - x => xs => p(x) && xs.every(Boolean) - ); -``` - - ```applescript -- allTree :: (a -> Bool) -> Tree a -> Bool on allTree(p, tree) @@ -28,4 +17,15 @@ on allTree(p, tree) end script |λ|(tree) of go end allTree +``` + + +```javascript +// allTree :: (a -> Bool) -> Tree a -> Bool +const allTree = p => + // True if p holds for all nodes of the + // tree to which allTree(p) is applied. + foldTree( + x => xs => p(x) && xs.every(Boolean) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/and.md b/MD applescript vs javascript/and.md index de23d718..9e6df871 100644 --- a/MD applescript vs javascript/and.md +++ b/MD applescript vs javascript/and.md @@ -1,11 +1,3 @@ -```javascript -// and :: [Bool] -> Bool -const and = xs => - // True unless any value in xs is false. - [...xs].every(Boolean); -``` - - ```applescript -- and :: [Bool] -> Bool on |and|(xs) @@ -15,4 +7,12 @@ on |and|(xs) end repeat return true end |and| +``` + + +```javascript +// and :: [Bool] -> Bool +const and = xs => + // True unless any value in xs is false. + [...xs].every(Boolean); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/any.md b/MD applescript vs javascript/any.md index 5a0664bd..c9e52431 100644 --- a/MD applescript vs javascript/any.md +++ b/MD applescript vs javascript/any.md @@ -1,12 +1,3 @@ -```javascript -// any :: (a -> Bool) -> [a] -> Bool -const any = p => - // True if p(x) holds for at least - // one item in xs. - xs => [...xs].some(p); -``` - - ```applescript -- any :: (a -> Bool) -> [a] -> Bool on any(p, xs) @@ -21,4 +12,13 @@ on any(p, xs) false end tell end any +``` + + +```javascript +// any :: (a -> Bool) -> [a] -> Bool +const any = p => + // True if p(x) holds for at least + // one item in xs. + xs => [...xs].some(p); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/anyTree.md b/MD applescript vs javascript/anyTree.md index f28fb794..1acd60d5 100644 --- a/MD applescript vs javascript/anyTree.md +++ b/MD applescript vs javascript/anyTree.md @@ -1,12 +1,3 @@ -```javascript -// anyTree :: (a -> Bool) -> Tree a -> Bool -const anyTree = p => - // True if p holds for any node of the - // tree to which anyTree(p) is applied. - foldTree(x => xs => p(x) || xs.some(Boolean)); -``` - - ```applescript -- anyTree :: (a -> Bool) -> Tree a -> Bool on anyTree(p, tree) @@ -26,4 +17,13 @@ on anyTree(p, tree) end script |λ|(tree) of go end anyTree +``` + + +```javascript +// anyTree :: (a -> Bool) -> Tree a -> Bool +const anyTree = p => + // True if p holds for any node of the + // tree to which anyTree(p) is applied. + foldTree(x => xs => p(x) || xs.some(Boolean)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/ap (<*>).md b/MD applescript vs javascript/ap (<*>).md index 8ba4e0d6..628a3a83 100644 --- a/MD applescript vs javascript/ap (<*>).md +++ b/MD applescript vs javascript/ap (<*>).md @@ -1,31 +1,3 @@ -```javascript -// ap (<*>) :: Monad m => m (a -> b) -> m a -> m b -const ap = mf => - // Applies wrapped functions to wrapped values, - // for example applying a list of functions to a list of values - // or applying Just(f) to Just(x), Right(f) to Right(x), - // f(x) to g(x) etc. - mx => { - const t = mx.type; - return ( - undefined !== t ? ( - 'Either' === t ? ( - apLR - ) : 'Maybe' === t ? ( - apMay - ) : 'Node' === t ? ( - apTree - ) : 'Tuple' === t ? ( - apTuple - ) : apList - ) : Array.isArray(mf) ? ( - apList - ) : apFn - )(mf)(mx); - }; -``` - - ```applescript -- Applies wrapped functions to wrapped values, -- for example applying a list of functions to a list of values @@ -51,4 +23,32 @@ on ap(mf, mx) end if end if end ap +``` + + +```javascript +// ap (<*>) :: Monad m => m (a -> b) -> m a -> m b +const ap = mf => + // Applies wrapped functions to wrapped values, + // for example applying a list of functions to a list of values + // or applying Just(f) to Just(x), Right(f) to Right(x), + // f(x) to g(x) etc. + mx => { + const t = mx.type; + return ( + undefined !== t ? ( + 'Either' === t ? ( + apLR + ) : 'Maybe' === t ? ( + apMay + ) : 'Node' === t ? ( + apTree + ) : 'Tuple' === t ? ( + apTuple + ) : apList + ) : Array.isArray(mf) ? ( + apList + ) : apFn + )(mf)(mx); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/apLR (<*>).md b/MD applescript vs javascript/apLR (<*>).md index 3513408b..bd36d04c 100644 --- a/MD applescript vs javascript/apLR (<*>).md +++ b/MD applescript vs javascript/apLR (<*>).md @@ -1,12 +1,3 @@ -```javascript -// apLR (<*>) :: Either e (a -> b) -> Either e a -> Either e b -const apLR = flr => - // Either a Left value, or the application of a - // function in Either to a value in Either. - liftA2LR(x => x)(flr); -``` - - ```applescript -- apLR (<*>) :: Either e (a -> b) -> Either e a -> Either e b on apLR(flr, lr) @@ -20,4 +11,13 @@ on apLR(flr, lr) flr end if end apLR +``` + + +```javascript +// apLR (<*>) :: Either e (a -> b) -> Either e a -> Either e b +const apLR = flr => + // Either a Left value, or the application of a + // function in Either to a value in Either. + liftA2LR(x => x)(flr); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/apList (<*>).md b/MD applescript vs javascript/apList (<*>).md index c7ac804c..f9156bed 100644 --- a/MD applescript vs javascript/apList (<*>).md +++ b/MD applescript vs javascript/apList (<*>).md @@ -1,14 +1,3 @@ -```javascript -// apList (<*>) :: [(a -> b)] -> [a] -> [b] -const apList = fs => - // The sequential application of each of a list - // of functions to each of a list of values. - // apList([x => 2 * x, x => 20 + x])([1, 2, 3]) - // -> [2, 4, 6, 21, 22, 23] - xs => fs.flatMap(f => xs.map(f)); -``` - - ```applescript -- e.g. [(*2),(/2), sqrt] <*> [1,2,3] -- --> ap([dbl, hlf, root], [1, 2, 3]) @@ -28,4 +17,15 @@ on apList(fs, xs) end repeat return lst end apList +``` + + +```javascript +// apList (<*>) :: [(a -> b)] -> [a] -> [b] +const apList = fs => + // The sequential application of each of a list + // of functions to each of a list of values. + // apList([x => 2 * x, x => 20 + x])([1, 2, 3]) + // -> [2, 4, 6, 21, 22, 23] + xs => fs.flatMap(f => xs.map(f)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/apMay (<*>).md b/MD applescript vs javascript/apMay (<*>).md index e6dd54b5..42b0ea05 100644 --- a/MD applescript vs javascript/apMay (<*>).md +++ b/MD applescript vs javascript/apMay (<*>).md @@ -1,12 +1,3 @@ -```javascript -// apMay (<*>) :: Maybe (a -> b) -> Maybe a -> Maybe b -const apMay = mf => - // Just an application of Maybe a function to - // to Maybe a value, or Nothing. - liftA2May(x => x)(mf); -``` - - ```applescript -- Maybe f applied to Maybe x, deriving a Maybe y -- apMay (<*>) :: Maybe (a -> b) -> Maybe a -> Maybe b @@ -17,4 +8,13 @@ on apMay(mf, mx) Just(|λ|(Just of mx) of mReturn(Just of mf)) end if end apMay +``` + + +```javascript +// apMay (<*>) :: Maybe (a -> b) -> Maybe a -> Maybe b +const apMay = mf => + // Just an application of Maybe a function to + // to Maybe a value, or Nothing. + liftA2May(x => x)(mf); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/apTree (<*>).md b/MD applescript vs javascript/apTree (<*>).md index 2ef5196b..e932206e 100644 --- a/MD applescript vs javascript/apTree (<*>).md +++ b/MD applescript vs javascript/apTree (<*>).md @@ -1,14 +1,3 @@ -```javascript -// apTree (<*>) :: Tree (a -> b) -> Tree a -> Tree b -const apTree = tf => - // A new tree derived by applying each of a tree - // of functions to each node value in another tree. - liftA2Tree( - x => x - )(tf); -``` - - ```applescript -- apTree (<*>) :: Tree (a -> b) -> Tree a -> Tree b on apTree(tf, tx) @@ -24,4 +13,15 @@ on apTree(tf, tx) return go's |λ|(tf) end apTree +``` + + +```javascript +// apTree (<*>) :: Tree (a -> b) -> Tree a -> Tree b +const apTree = tf => + // A new tree derived by applying each of a tree + // of functions to each node value in another tree. + liftA2Tree( + x => x + )(tf); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/apTuple (<*>).md b/MD applescript vs javascript/apTuple (<*>).md index f294b6c8..a6342997 100644 --- a/MD applescript vs javascript/apTuple (<*>).md +++ b/MD applescript vs javascript/apTuple (<*>).md @@ -1,13 +1,13 @@ -```javascript -// apTuple (<*>) :: Monoid m => (m, (a -> b)) -> (m, a) -> (m, b) -const apTuple = tpl => - liftA2Tuple(x => x)(tpl); -``` - - ```applescript -- apTuple (<*>) :: Monoid m => (m, (a -> b)) -> (m, a) -> (m, b) on apTuple(tf, tx) Tuple(mappend(|1| of tf, |1| of tx), |λ|(|2| of tx) of mReturn(|2| of tf)) end apTuple +``` + + +```javascript +// apTuple (<*>) :: Monoid m => (m, (a -> b)) -> (m, a) -> (m, b) +const apTuple = tpl => + liftA2Tuple(x => x)(tpl); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/append (++).md b/MD applescript vs javascript/append (++).md index 6f12f093..a907e3b4 100644 --- a/MD applescript vs javascript/append (++).md +++ b/MD applescript vs javascript/append (++).md @@ -1,12 +1,3 @@ -```javascript -// append (++) :: [a] -> [a] -> [a] -const append = xs => - // A list defined by the - // concatenation of two others. - ys => xs.concat(ys); -``` - - ```applescript -- Append two lists. -- append (++) :: [a] -> [a] -> [a] @@ -14,4 +5,13 @@ const append = xs => on append(xs, ys) xs & ys end append +``` + + +```javascript +// append (++) :: [a] -> [a] -> [a] +const append = xs => + // A list defined by the + // concatenation of two others. + ys => xs.concat(ys); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/appendFile.md b/MD applescript vs javascript/appendFile.md index f9b42503..4f4dd939 100644 --- a/MD applescript vs javascript/appendFile.md +++ b/MD applescript vs javascript/appendFile.md @@ -1,40 +1,3 @@ -```javascript -// appendFile :: FilePath -> String -> IO Bool -const appendFile = fp => - // The file at fp updated with a new string - // appended to its existing contents. - txt => { - const - oFullPath = ObjC.wrap(fp) - .stringByStandardizingPath, - ref = Ref(); - return $.NSFileManager.defaultManager - .fileExistsAtPathIsDirectory( - oFullPath - .stringByStandardizingPath, ref - ) ? ( - 0 === ref[0] ? (() => { - const // Not a directory - oData = ObjC.wrap(txt) - .dataUsingEncoding($.NSUTF8StringEncoding), - h = $.NSFileHandle.fileHandleForWritingAtPath( - oFullPath - ); - return ( - h.seekToEndOfFile, // Effect, and - h.writeData(oData), - h.closeFile, - true // value. - ); - })() : false // Text appending to directory is undefined - ) : doesDirectoryExist(takeDirectory(ObjC.unwrap(fp))) ? ( - writeFile(oFullPath)(txt), // Effect, and - true // value. - ) : false; - }; -``` - - ```applescript -- Write a string to the end of a file. -- Returns true if the path exists @@ -69,4 +32,41 @@ on appendFile(strPath, txt) end if end if end appendFile +``` + + +```javascript +// appendFile :: FilePath -> String -> IO Bool +const appendFile = fp => + // The file at fp updated with a new string + // appended to its existing contents. + txt => { + const + oFullPath = ObjC.wrap(fp) + .stringByStandardizingPath, + ref = Ref(); + return $.NSFileManager.defaultManager + .fileExistsAtPathIsDirectory( + oFullPath + .stringByStandardizingPath, ref + ) ? ( + 0 === ref[0] ? (() => { + const // Not a directory + oData = ObjC.wrap(txt) + .dataUsingEncoding($.NSUTF8StringEncoding), + h = $.NSFileHandle.fileHandleForWritingAtPath( + oFullPath + ); + return ( + h.seekToEndOfFile, // Effect, and + h.writeData(oData), + h.closeFile, + true // value. + ); + })() : false // Text appending to directory is undefined + ) : doesDirectoryExist(takeDirectory(ObjC.unwrap(fp))) ? ( + writeFile(oFullPath)(txt), // Effect, and + true // value. + ) : false; + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/appendFileMay.md b/MD applescript vs javascript/appendFileMay.md index c4124644..f386dab9 100644 --- a/MD applescript vs javascript/appendFileMay.md +++ b/MD applescript vs javascript/appendFileMay.md @@ -1,3 +1,40 @@ +```applescript +-- Write a string to the end of a file. +-- Returns a Just FilePath value if the +-- path exists and the write succeeded. +-- Otherwise returns Nothing. +-- appendFileMay :: FilePath -> String -> Maybe IO FilePath +on appendFileMay(strPath, txt) + set ca to current application + set oFullPath to (ca's NSString's stringWithString:strPath)'s ¬ + stringByStandardizingPath + set strFullPath to oFullPath as string + set {blnExists, intFolder} to (ca's NSFileManager's defaultManager()'s ¬ + fileExistsAtPath:oFullPath isDirectory:(reference)) + if blnExists then + if 0 = intFolder then -- Not a directory + set oData to (ca's NSString's stringWithString:txt)'s ¬ + dataUsingEncoding:(ca's NSUTF8StringEncoding) + set h to ca's NSFileHandle's fileHandleForWritingAtPath:oFullPath + h's seekToEndOfFile + h's writeData:oData + h's closeFile() + Just(strFullPath) + else + Nothing() + end if + else + if doesDirectoryExist(takeDirectory(strFullPath)) then + writeFile(oFullPath, txt) + Just(strFullPath) + else + Nothing() + end if + end if +end appendFileMay +``` + + ```javascript // appendFileMay :: FilePath -> String -> Maybe IO FilePath const appendFileMay = strPath => @@ -38,41 +75,4 @@ const appendFileMay = strPath => Just(strFullPath) // value ) : Nothing(); }; -``` - - -```applescript --- Write a string to the end of a file. --- Returns a Just FilePath value if the --- path exists and the write succeeded. --- Otherwise returns Nothing. --- appendFileMay :: FilePath -> String -> Maybe IO FilePath -on appendFileMay(strPath, txt) - set ca to current application - set oFullPath to (ca's NSString's stringWithString:strPath)'s ¬ - stringByStandardizingPath - set strFullPath to oFullPath as string - set {blnExists, intFolder} to (ca's NSFileManager's defaultManager()'s ¬ - fileExistsAtPath:oFullPath isDirectory:(reference)) - if blnExists then - if 0 = intFolder then -- Not a directory - set oData to (ca's NSString's stringWithString:txt)'s ¬ - dataUsingEncoding:(ca's NSUTF8StringEncoding) - set h to ca's NSFileHandle's fileHandleForWritingAtPath:oFullPath - h's seekToEndOfFile - h's writeData:oData - h's closeFile() - Just(strFullPath) - else - Nothing() - end if - else - if doesDirectoryExist(takeDirectory(strFullPath)) then - writeFile(oFullPath, txt) - Just(strFullPath) - else - Nothing() - end if - end if -end appendFileMay ``` \ No newline at end of file diff --git a/MD applescript vs javascript/appendGen.md b/MD applescript vs javascript/appendGen.md index 59b55c5d..e90a2c75 100644 --- a/MD applescript vs javascript/appendGen.md +++ b/MD applescript vs javascript/appendGen.md @@ -1,20 +1,3 @@ -```javascript -// appendGen (++) :: Gen [a] -> Gen [a] -> Gen [a] -const appendGen = xs => - // A new generator composed from the - // concatenation of two existing generators. - function* (ys) { - for (let vs of [xs, ys]) { - let nxt = vs.next(); - while (!nxt.done) { - yield nxt.value; - nxt = vs.next(); - } - } - }; -``` - - ```applescript -- appendGen (++) :: Gen [a] -> Gen [a] -> Gen [a] on appendGen(xs, ys) @@ -31,4 +14,21 @@ on appendGen(xs, ys) end |λ| end script end appendGen +``` + + +```javascript +// appendGen (++) :: Gen [a] -> Gen [a] -> Gen [a] +const appendGen = xs => + // A new generator composed from the + // concatenation of two existing generators. + function* (ys) { + for (let vs of [xs, ys]) { + let nxt = vs.next(); + while (!nxt.done) { + yield nxt.value; + nxt = vs.next(); + } + } + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/apply ($).md b/MD applescript vs javascript/apply ($).md index be76ee77..ff0d4b57 100644 --- a/MD applescript vs javascript/apply ($).md +++ b/MD applescript vs javascript/apply ($).md @@ -1,14 +1,14 @@ -```javascript -// apply ($) :: (a -> b) -> a -> b -const apply = f => - // Application operator. - x => f(x); -``` - - ```applescript -- apply ($) :: (a -> b) -> a -> b on apply(f, x) mReturn(f)'s |λ|(x) end apply +``` + + +```javascript +// apply ($) :: (a -> b) -> a -> b +const apply = f => + // Application operator. + x => f(x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/applyN.md b/MD applescript vs javascript/applyN.md index fed0b770..d6c81e42 100644 --- a/MD applescript vs javascript/applyN.md +++ b/MD applescript vs javascript/applyN.md @@ -1,15 +1,3 @@ -```javascript -// applyN :: Int -> (a -> a) -> a -> a -const applyN = n => - // The value of n applications of f to x. - // (Church numeral n) - f => x => Array.from({ - length: n - }, () => f) - .reduce((a, g) => g(a), x); -``` - - ```applescript -- applyN :: Int -> (a -> a) -> a -> a on applyN(n, f, x) @@ -20,4 +8,16 @@ on applyN(n, f, x) end script foldl(go, x, replicate(n, f)) end applyN +``` + + +```javascript +// applyN :: Int -> (a -> a) -> a -> a +const applyN = n => + // The value of n applications of f to x. + // (Church numeral n) + f => x => Array.from({ + length: n + }, () => f) + .reduce((a, g) => g(a), x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/approxRatio.md b/MD applescript vs javascript/approxRatio.md index ec312c37..4b17f4f7 100644 --- a/MD applescript vs javascript/approxRatio.md +++ b/MD applescript vs javascript/approxRatio.md @@ -1,25 +1,3 @@ -```javascript -// approxRatio :: Float -> Float -> Ratio -const approxRatio = epsilon => - // An ratio derived by approximation - // (at granularity epsilon) to the float n. - n => { - const - gcde = (e, x, y) => { - const _gcd = (a, b) => (b < e ? a : _gcd(b, a % b)); - return _gcd(abs(x), abs(y)); - }, - c = gcde(Boolean(epsilon) ? epsilon : (1 / 10000), 1, abs(n)), - r = ratio(quot(abs(n))(c))(quot(1, c)); - return { - type: 'Ratio', - n: r.n * signum(n), - d: r.d - }; - }; -``` - - ```applescript -- approxRatio :: Float -> Float -> Ratio on approxRatio(epsilon, n) @@ -47,4 +25,26 @@ on approxRatio(epsilon, n) set c to |λ|(e, 1, n) of gcde Ratio((n div c), (1 div c)) end approxRatio +``` + + +```javascript +// approxRatio :: Float -> Float -> Ratio +const approxRatio = epsilon => + // An ratio derived by approximation + // (at granularity epsilon) to the float n. + n => { + const + gcde = (e, x, y) => { + const _gcd = (a, b) => (b < e ? a : _gcd(b, a % b)); + return _gcd(abs(x), abs(y)); + }, + c = gcde(Boolean(epsilon) ? epsilon : (1 / 10000), 1, abs(n)), + r = ratio(quot(abs(n))(c))(quot(1, c)); + return { + type: 'Ratio', + n: r.n * signum(n), + d: r.d + }; + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/argvLength.md b/MD applescript vs javascript/argvLength.md index e5e57815..fad166b9 100644 --- a/MD applescript vs javascript/argvLength.md +++ b/MD applescript vs javascript/argvLength.md @@ -1,10 +1,3 @@ -```javascript -// argvLength :: Function -> Int -const argvLength = f => - f.length; -``` - - ```applescript -- argvLength :: Function -> Int on argvLength(h) @@ -18,4 +11,11 @@ on argvLength(h) length of xs end try end argvLength +``` + + +```javascript +// argvLength :: Function -> Int +const argvLength = f => + f.length; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/assocs.md b/MD applescript vs javascript/assocs.md index 15050832..33b9dd32 100644 --- a/MD applescript vs javascript/assocs.md +++ b/MD applescript vs javascript/assocs.md @@ -1,12 +1,3 @@ -```javascript -// assocs :: Map k a -> [(k, a)] -const assocs = m => - Object.entries(m).map( - kv => Tuple(...kv) - ); -``` - - ```applescript -- assocs :: Map k a -> [(k, a)] on assocs(m) @@ -22,4 +13,13 @@ on assocs(m) end script concatMap(go, keys(m)) end assocs +``` + + +```javascript +// assocs :: Map k a -> [(k, a)] +const assocs = m => + Object.entries(m).map( + kv => Tuple(...kv) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/base64decode.md b/MD applescript vs javascript/base64decode.md index 68972ce6..032f3ada 100644 --- a/MD applescript vs javascript/base64decode.md +++ b/MD applescript vs javascript/base64decode.md @@ -1,17 +1,3 @@ -```javascript -// base64decode :: String -> String -const base64decode = s => - ObjC.unwrap( - $.NSString.alloc.initWithDataEncoding( - $.NSData.alloc.initWithBase64EncodedStringOptions( - s, 0 - ), - $.NSUTF8StringEncoding - ) - ); -``` - - ```applescript -- base64decode :: String -> String on base64decode(s) @@ -24,4 +10,18 @@ on base64decode(s) options:(ignore)))) encoding:ignore)) as text end tell end base64decode +``` + + +```javascript +// base64decode :: String -> String +const base64decode = s => + ObjC.unwrap( + $.NSString.alloc.initWithDataEncoding( + $.NSData.alloc.initWithBase64EncodedStringOptions( + s, 0 + ), + $.NSUTF8StringEncoding + ) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/base64encode.md b/MD applescript vs javascript/base64encode.md index ad05efa1..3b31cd1e 100644 --- a/MD applescript vs javascript/base64encode.md +++ b/MD applescript vs javascript/base64encode.md @@ -1,15 +1,3 @@ -```javascript -// base64encode :: String -> String -const base64encode = s => - ObjC.unwrap( - $.NSString.stringWithString(s) - .dataUsingEncoding( - $.NSUTF8StringEncoding - ).base64EncodedStringWithOptions(0) - ); -``` - - ```applescript -- base64encode :: String -> String on base64encode(s) @@ -20,4 +8,16 @@ on base64encode(s) (stringWithString_(s) of its NSString) as string end tell end base64encode +``` + + +```javascript +// base64encode :: String -> String +const base64encode = s => + ObjC.unwrap( + $.NSString.stringWithString(s) + .dataUsingEncoding( + $.NSUTF8StringEncoding + ).base64EncodedStringWithOptions(0) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/bimap.md b/MD applescript vs javascript/bimap.md index 7b6e7bc1..4bf12919 100644 --- a/MD applescript vs javascript/bimap.md +++ b/MD applescript vs javascript/bimap.md @@ -1,17 +1,3 @@ -```javascript -// bimap :: (a -> b) -> (c -> d) -> (a, c) -> (b, d) -const bimap = f => - // Tuple instance of bimap. - // A tuple of the application of f and g to the - // first and second values respectively. - g => tpl => 2 !== tpl.length ? ( - bimapN(f)(g)(tpl) - ) : Tuple(f(tpl[0]))( - g(tpl[1]) - ); -``` - - ```applescript -- bimap :: (a -> b) -> (c -> d) -> (a, c) -> (b, d) on bimap(f, g) @@ -25,4 +11,18 @@ on bimap(f, g) end |λ| end script end bimap +``` + + +```javascript +// bimap :: (a -> b) -> (c -> d) -> (a, c) -> (b, d) +const bimap = f => + // Tuple instance of bimap. + // A tuple of the application of f and g to the + // first and second values respectively. + g => tpl => 2 !== tpl.length ? ( + bimapN(f)(g)(tpl) + ) : Tuple(f(tpl[0]))( + g(tpl[1]) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/bimapLR.md b/MD applescript vs javascript/bimapLR.md index 51e89aa5..d4440bb0 100644 --- a/MD applescript vs javascript/bimapLR.md +++ b/MD applescript vs javascript/bimapLR.md @@ -1,15 +1,3 @@ -```javascript -// bimapLR :: (a -> b) -> (c -> d) -> ֵEither ֵֵa c -> Either b d -const bimapLR = f => - // Instance of bimap for Either values. - // Either the application of f to a Left value, - // or the application of g to a Right value. - g => lr => undefined !== lr.Left ? ( - Left(f(lr.Left)) - ) : Right(g(lr.Right)); -``` - - ```applescript -- bimapLR :: (a -> b) -> (c -> d) -> ֵEither ֵֵa c -> Either b d on bimapLR(f, g) @@ -23,4 +11,16 @@ on bimapLR(f, g) end |λ| end script end bimapLR +``` + + +```javascript +// bimapLR :: (a -> b) -> (c -> d) -> ֵEither ֵֵa c -> Either b d +const bimapLR = f => + // Instance of bimap for Either values. + // Either the application of f to a Left value, + // or the application of g to a Right value. + g => lr => undefined !== lr.Left ? ( + Left(f(lr.Left)) + ) : Right(g(lr.Right)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/bimapN.md b/MD applescript vs javascript/bimapN.md index 3c013760..e67f8504 100644 --- a/MD applescript vs javascript/bimapN.md +++ b/MD applescript vs javascript/bimapN.md @@ -1,3 +1,16 @@ +```applescript +-- bimapN :: (a -> b) -> (c -> d) -> TupleN -> TupleN +on bimapN(f, g, tplN) + set z to length of tplN + set k1 to (z - 1) as string + set k2 to z as string + + insertDict(k2, mReturn(g)'s |λ|(Just of lookupDict(k2, tplN)), ¬ + insertDict(k1, mReturn(f)'s |λ|(Just of lookupDict(k1, tplN)), tplN)) +end bimapN +``` + + ```javascript // bimapN :: (a -> b) -> (c -> d) -> TupleN -> TupleN const bimapN = f => @@ -14,17 +27,4 @@ const bimapN = f => ) ) : undefined; }; -``` - - -```applescript --- bimapN :: (a -> b) -> (c -> d) -> TupleN -> TupleN -on bimapN(f, g, tplN) - set z to length of tplN - set k1 to (z - 1) as string - set k2 to z as string - - insertDict(k2, mReturn(g)'s |λ|(Just of lookupDict(k2, tplN)), ¬ - insertDict(k1, mReturn(f)'s |λ|(Just of lookupDict(k1, tplN)), tplN)) -end bimapN ``` \ No newline at end of file diff --git a/MD applescript vs javascript/bind (>>=).md b/MD applescript vs javascript/bind (>>=).md index 0a745b7a..82f18596 100644 --- a/MD applescript vs javascript/bind (>>=).md +++ b/MD applescript vs javascript/bind (>>=).md @@ -1,23 +1,3 @@ -```javascript -// bind (>>=) :: Monad m => m a -> (a -> m b) -> m b -const bind = m => - mf => (Array.isArray(m) ? ( - bindList - ) : (() => { - const t = m.type; - return 'Either' === t ? ( - bindLR - ) : 'Maybe' === t ? ( - bindMay - ) : 'Tuple' === t ? ( - bindTuple - ) : ('function' === typeof m) ? ( - bindFn - ) : undefined; - })()(m)(mf)); -``` - - ```applescript -- bind (>>=) :: Monad m => m a -> (a -> m b) -> m b on bind(m, mf) @@ -46,4 +26,24 @@ on bind(m, mf) missing value end if end bind +``` + + +```javascript +// bind (>>=) :: Monad m => m a -> (a -> m b) -> m b +const bind = m => + mf => (Array.isArray(m) ? ( + bindList + ) : (() => { + const t = m.type; + return 'Either' === t ? ( + bindLR + ) : 'Maybe' === t ? ( + bindMay + ) : 'Tuple' === t ? ( + bindTuple + ) : ('function' === typeof m) ? ( + bindFn + ) : undefined; + })()(m)(mf)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/bindFn (>>=).md b/MD applescript vs javascript/bindFn (>>=).md index 07f225bf..0c891572 100644 --- a/MD applescript vs javascript/bindFn (>>=).md +++ b/MD applescript vs javascript/bindFn (>>=).md @@ -1,11 +1,3 @@ -```javascript -// bindFn (>>=) :: (a -> b) -> (b -> a -> c) -> a -> c -const bindFn = f => - // Binary operator applied over f x and x. - bop => x => bop(f(x))(x); -``` - - ```applescript -- bindFn (>>=) :: (a -> b) -> (b -> a -> c) -> a -> c on bindFn(f, bop) @@ -22,4 +14,12 @@ on bindFn(f, bop) end |λ| end script end bindFn +``` + + +```javascript +// bindFn (>>=) :: (a -> b) -> (b -> a -> c) -> a -> c +const bindFn = f => + // Binary operator applied over f x and x. + bop => x => bop(f(x))(x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/bindLR (>>=).md b/MD applescript vs javascript/bindLR (>>=).md index abd68562..cbcc7442 100644 --- a/MD applescript vs javascript/bindLR (>>=).md +++ b/MD applescript vs javascript/bindLR (>>=).md @@ -1,13 +1,3 @@ -```javascript -// bindLR (>>=) :: Either a -> -// (a -> Either b) -> Either b -const bindLR = m => - mf => undefined !== m.Left ? ( - m - ) : mf(m.Right); -``` - - ```applescript -- bindLR (>>=) :: Either a -> (a -> Either b) -> Either b on bindLR(m, mf) @@ -17,4 +7,14 @@ on bindLR(m, mf) mReturn(mf)'s |λ|(|Right| of m) end if end bindLR +``` + + +```javascript +// bindLR (>>=) :: Either a -> +// (a -> Either b) -> Either b +const bindLR = m => + mf => undefined !== m.Left ? ( + m + ) : mf(m.Right); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/bindList (>>=).md b/MD applescript vs javascript/bindList (>>=).md index 42a1d892..df2e153f 100644 --- a/MD applescript vs javascript/bindList (>>=).md +++ b/MD applescript vs javascript/bindList (>>=).md @@ -1,10 +1,3 @@ -```javascript -// bindList (>>=) :: [a] -> (a -> [b]) -> [b] -const bindList = xs => - mf => [...xs].flatMap(mf); -``` - - ```applescript -- bindList (>>=) :: [a] -> (a -> [b]) -> [b] on bindList(xs, f) @@ -16,4 +9,11 @@ on bindList(xs, f) end tell return acc end bindList +``` + + +```javascript +// bindList (>>=) :: [a] -> (a -> [b]) -> [b] +const bindList = xs => + mf => [...xs].flatMap(mf); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/bindMay (>>=).md b/MD applescript vs javascript/bindMay (>>=).md index 6030d721..e1fdefba 100644 --- a/MD applescript vs javascript/bindMay (>>=).md +++ b/MD applescript vs javascript/bindMay (>>=).md @@ -1,14 +1,3 @@ -```javascript -// bindMay (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b -const bindMay = mb => - // Nothing if mb is Nothing, or the application of the - // (a -> Maybe b) function mf to the contents of mb. - mf => mb.Nothing ? ( - mb - ) : mf(mb.Just); -``` - - ```applescript -- bindMay (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b on bindMay(mb, mf) @@ -23,4 +12,15 @@ on bindMay(mb, mf) tell mReturn(mf) to |λ|(Just of mb) end if end bindMay +``` + + +```javascript +// bindMay (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b +const bindMay = mb => + // Nothing if mb is Nothing, or the application of the + // (a -> Maybe b) function mf to the contents of mb. + mf => mb.Nothing ? ( + mb + ) : mf(mb.Just); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/bindTuple (>>=).md b/MD applescript vs javascript/bindTuple (>>=).md index 727b06b8..f0cb6527 100644 --- a/MD applescript vs javascript/bindTuple (>>=).md +++ b/MD applescript vs javascript/bindTuple (>>=).md @@ -1,3 +1,12 @@ +```applescript +-- bindTuple (>>=) :: Monoid a => (a, a) -> (a -> (a, b)) -> (a, b) +on bindTuple(tpl, f) + set t2 to mReturn(f)'s |λ|(|2| of tpl) + Tuple(mappend(|1| of tpl, |1| of t2), |2| of t2) +end bindTuple +``` + + ```javascript // bindTuple (>>=) :: Monoid a => (a, a) -> (a -> (a, b)) -> (a, b) const bindTuple = tpl => @@ -7,13 +16,4 @@ const bindTuple = tpl => t2[1] ); }; -``` - - -```applescript --- bindTuple (>>=) :: Monoid a => (a, a) -> (a -> (a, b)) -> (a, b) -on bindTuple(tpl, f) - set t2 to mReturn(f)'s |λ|(|2| of tpl) - Tuple(mappend(|1| of tpl, |1| of t2), |2| of t2) -end bindTuple ``` \ No newline at end of file diff --git a/MD applescript vs javascript/bool.md b/MD applescript vs javascript/bool.md index c5b009eb..85100b59 100644 --- a/MD applescript vs javascript/bool.md +++ b/MD applescript vs javascript/bool.md @@ -1,10 +1,3 @@ -```javascript -// bool :: a -> a -> Bool -> a -const bool = f => - t => p => p ? t : f; -``` - - ```applescript -- bool :: a -> a -> Bool -> a on bool(ff, tf) @@ -26,4 +19,11 @@ on bool(ff, tf) end |λ| end script end bool +``` + + +```javascript +// bool :: a -> a -> Bool -> a +const bool = f => + t => p => p ? t : f; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/break.md b/MD applescript vs javascript/break.md index da0a0038..162499ee 100644 --- a/MD applescript vs javascript/break.md +++ b/MD applescript vs javascript/break.md @@ -1,19 +1,3 @@ -```javascript -// break :: (a -> Bool) -> [a] -> ([a], [a]) -const break_ = p => - xs => { - const - iLast = xs.length - 1, - ys = [...xs]; - return splitAt( - until(i => iLast < i || p(ys[i]))( - i => 1 + i - )(0) - )(ys); - }; -``` - - ```applescript -- break :: (a -> Bool) -> [a] -> ([a], [a]) on break(p, xs) @@ -37,4 +21,20 @@ on break(p, xs) Tuple(xs, {}) end if end break +``` + + +```javascript +// break :: (a -> Bool) -> [a] -> ([a], [a]) +const break_ = p => + xs => { + const + iLast = xs.length - 1, + ys = [...xs]; + return splitAt( + until(i => iLast < i || p(ys[i]))( + i => 1 + i + )(0) + )(ys); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/breakOn.md b/MD applescript vs javascript/breakOn.md index d196e660..4b881aa1 100644 --- a/MD applescript vs javascript/breakOn.md +++ b/MD applescript vs javascript/breakOn.md @@ -1,16 +1,3 @@ -```javascript -// breakOn :: String -> String -> (String, String) -const breakOn = pat => - // Needle -> Haystack -> (prefix before match, match + rest) - src => 0 < pat.length ? (() => { - const xs = src.split(pat); - return 1 < xs.length ? Tuple( - xs[0], src.slice(xs[0].length) - ) : Tuple(src)(''); - })() : undefined; -``` - - ```applescript -- non null needle -> haystack -> (prefix before match, match + rest) -- breakOn :: String -> String -> (String, String) @@ -34,4 +21,17 @@ on breakOn(pat, src) missing value end if end breakOn +``` + + +```javascript +// breakOn :: String -> String -> (String, String) +const breakOn = pat => + // Needle -> Haystack -> (prefix before match, match + rest) + src => 0 < pat.length ? (() => { + const xs = src.split(pat); + return 1 < xs.length ? Tuple( + xs[0], src.slice(xs[0].length) + ) : Tuple(src)(''); + })() : undefined; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/breakOnAll.md b/MD applescript vs javascript/breakOnAll.md index eb56e6a7..45588a3a 100644 --- a/MD applescript vs javascript/breakOnAll.md +++ b/MD applescript vs javascript/breakOnAll.md @@ -1,20 +1,3 @@ -```javascript -// breakOnAll :: String -> String -> [(String, String)] -const breakOnAll = pat => - src => '' !== pat ? ( - src.split(pat) - .reduce((a, x, i, xs) => - 0 < i ? ( - a.concat([ - Tuple(xs.slice(0, i).join(pat))( - pat + xs.slice(i).join(pat) - ) - ]) - ) : a, []) - ) : undefined; -``` - - ```applescript -- breakOnAll "/" "a/b/c/" -- ==> [("a", "/b/c/"), ("a/b", "/c/"), ("a/b/c", "/")] @@ -36,4 +19,21 @@ on breakOnAll(pat, src) missing value end if end breakOnAll +``` + + +```javascript +// breakOnAll :: String -> String -> [(String, String)] +const breakOnAll = pat => + src => '' !== pat ? ( + src.split(pat) + .reduce((a, x, i, xs) => + 0 < i ? ( + a.concat([ + Tuple(xs.slice(0, i).join(pat))( + pat + xs.slice(i).join(pat) + ) + ]) + ) : a, []) + ) : undefined; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/breakOnMay.md b/MD applescript vs javascript/breakOnMay.md index 10af86b3..53bcbbf1 100644 --- a/MD applescript vs javascript/breakOnMay.md +++ b/MD applescript vs javascript/breakOnMay.md @@ -1,16 +1,3 @@ -```javascript -// breakOnMay :: String -> String -> Maybe (String, String) -const breakOnMay = pat => - // Needle -> Haystack -> maybe (prefix before match, match + rest) - src => Boolean(pat) ? (() => { - const xs = src.split(pat); - return Just(0 < xs.length ? Tuple( - xs[0], src.slice(xs[0].length) - ) : Tuple(src)('')); - })() : Nothing(); -``` - - ```applescript -- needle -> haystack -> maybe (prefix before match, match + rest) -- breakOnMay :: String -> String -> Maybe (String, String) @@ -32,4 +19,17 @@ on breakOnMay(pat, src) Nothing() end if end breakOnMay +``` + + +```javascript +// breakOnMay :: String -> String -> Maybe (String, String) +const breakOnMay = pat => + // Needle -> Haystack -> maybe (prefix before match, match + rest) + src => Boolean(pat) ? (() => { + const xs = src.split(pat); + return Just(0 < xs.length ? Tuple( + xs[0], src.slice(xs[0].length) + ) : Tuple(src)('')); + })() : Nothing(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/bulleted.md b/MD applescript vs javascript/bulleted.md index dd29987e..92dbcd5b 100644 --- a/MD applescript vs javascript/bulleted.md +++ b/MD applescript vs javascript/bulleted.md @@ -1,12 +1,3 @@ -```javascript -// bulleted :: String -> String -> String -const bulleted = strTab => - s => s.split(/[\r\n]/).map( - x => '' !== x ? strTab + '- ' + x : x - ).join('\n'); -``` - - ```applescript -- bulleted :: String -> String -> String on bulleted(strIndent, s) @@ -21,4 +12,13 @@ on bulleted(strIndent, s) end script unlines(map(go, paragraphs of s)) end bulleted +``` + + +```javascript +// bulleted :: String -> String -> String +const bulleted = strTab => + s => s.split(/[\r\n]/).map( + x => '' !== x ? strTab + '- ' + x : x + ).join('\n'); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/cartesianProduct.md b/MD applescript vs javascript/cartesianProduct.md index f04ded64..9598d927 100644 --- a/MD applescript vs javascript/cartesianProduct.md +++ b/MD applescript vs javascript/cartesianProduct.md @@ -1,16 +1,3 @@ -```javascript -// cartesianProduct :: [a] -> [b] -> [[a, b]] -const cartesianProduct = xs => - ys => ( - bs => [...xs].flatMap( - x => bs.flatMap(b => [ - [x].concat(b) - ]) - ) - )([...ys]); -``` - - ```applescript -- cartesianProduct :: [a] -> [b] -> [[a, b]] on cartesianProduct(xs, ys) @@ -26,4 +13,17 @@ on cartesianProduct(xs, ys) end script concatMap(result, xs) end cartesianProduct +``` + + +```javascript +// cartesianProduct :: [a] -> [b] -> [[a, b]] +const cartesianProduct = xs => + ys => ( + bs => [...xs].flatMap( + x => bs.flatMap(b => [ + [x].concat(b) + ]) + ) + )([...ys]); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/caseOf.md b/MD applescript vs javascript/caseOf.md index 30c143cd..8b8cf151 100644 --- a/MD applescript vs javascript/caseOf.md +++ b/MD applescript vs javascript/caseOf.md @@ -1,3 +1,15 @@ +```applescript +-- List of (Predicate, value) tuples -> Default value -> Value to test -> Output value +-- caseOf :: [(a -> Bool, b)] -> b -> a -> b +on caseOf (pvs, otherwise, x) + repeat with tpl in pvs + if mReturn(|1| of tpl)'s |λ|(x) then return |2| of tpl + end repeat + return otherwise +end caseOf +``` + + ```javascript // caseOf :: [(a -> Bool, b)] -> b -> a -> b const caseOf = pvs => @@ -10,16 +22,4 @@ const caseOf = pvs => ) : a, Nothing()); return mb.Nothing ? otherwise : mb.Just; }; -``` - - -```applescript --- List of (Predicate, value) tuples -> Default value -> Value to test -> Output value --- caseOf :: [(a -> Bool, b)] -> b -> a -> b -on caseOf (pvs, otherwise, x) - repeat with tpl in pvs - if mReturn(|1| of tpl)'s |λ|(x) then return |2| of tpl - end repeat - return otherwise -end caseOf ``` \ No newline at end of file diff --git a/MD applescript vs javascript/catMaybes.md b/MD applescript vs javascript/catMaybes.md index 7a911455..4e87285b 100644 --- a/MD applescript vs javascript/catMaybes.md +++ b/MD applescript vs javascript/catMaybes.md @@ -1,10 +1,3 @@ -```javascript -// catMaybes :: [Maybe a] -> [a] -const catMaybes = mbs => - mbs.flatMap(m => m.Nothing ? [] : [m.Just]); -``` - - ```applescript -- catMaybes :: [Maybe a] -> [a] on catMaybes(mbs) @@ -19,4 +12,11 @@ on catMaybes(mbs) end script concatMap(emptyOrListed, mbs) end catMaybes +``` + + +```javascript +// catMaybes :: [Maybe a] -> [a] +const catMaybes = mbs => + mbs.flatMap(m => m.Nothing ? [] : [m.Just]); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/ceiling.md b/MD applescript vs javascript/ceiling.md index b3699a08..59e6d21d 100644 --- a/MD applescript vs javascript/ceiling.md +++ b/MD applescript vs javascript/ceiling.md @@ -1,15 +1,3 @@ -```javascript -// ceiling :: Num -> Int -const ceiling = x => { - // The least integer not less than x. - const - nr = properFraction(x), - n = nr[0]; - return 0 < nr[1] ? 1 + n : n; -}; -``` - - ```applescript -- ceiling :: Num -> Int on ceiling(x) @@ -21,4 +9,16 @@ on ceiling(x) n end if end ceiling +``` + + +```javascript +// ceiling :: Num -> Int +const ceiling = x => { + // The least integer not less than x. + const + nr = properFraction(x), + n = nr[0]; + return 0 < nr[1] ? 1 + n : n; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/center.md b/MD applescript vs javascript/center.md index db3c84a1..8064f220 100644 --- a/MD applescript vs javascript/center.md +++ b/MD applescript vs javascript/center.md @@ -1,17 +1,3 @@ -```javascript -// center :: Int -> Char -> String -> String -const center = n => - // Size of space -> filler Char -> String -> Centered String - c => s => { - const gap = n - s.length; - return 0 < gap ? (() => { - const pre = c.repeat(Math.floor(gap / 2)); - return pre + s + pre + c.repeat(gap % 2); - })() : s; - }; -``` - - ```applescript -- center :: Int -> Char -> String -> String on |center|(n, cFiller, strText) @@ -28,4 +14,18 @@ on |center|(n, cFiller, strText) strText end if end |center| +``` + + +```javascript +// center :: Int -> Char -> String -> String +const center = n => + // Size of space -> filler Char -> String -> Centered String + c => s => { + const gap = n - s.length; + return 0 < gap ? (() => { + const pre = c.repeat(Math.floor(gap / 2)); + return pre + s + pre + c.repeat(gap % 2); + })() : s; + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/chars.md b/MD applescript vs javascript/chars.md index bebe5118..c4a9a9d3 100644 --- a/MD applescript vs javascript/chars.md +++ b/MD applescript vs javascript/chars.md @@ -1,13 +1,13 @@ -```javascript -// chars :: String -> [Char] -const chars = s => - s.split(''); -``` - - ```applescript -- chars :: String -> [Char] on chars(s) characters of s end chars +``` + + +```javascript +// chars :: String -> [Char] +const chars = s => + s.split(''); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/chop.md b/MD applescript vs javascript/chop.md index 77639595..8033e44b 100644 --- a/MD applescript vs javascript/chop.md +++ b/MD applescript vs javascript/chop.md @@ -1,19 +1,3 @@ -```javascript -// chop :: ([a] -> (b, [a])) -> [a] -> [b] -const chop = f => - // A segmentation of xs by tail recursion with a - // function which returns a (prefix, residue) tuple. - xs => { - const go = xs => - 0 < xs.length ? (() => { - const [b, bs] = Array.from(f(xs)); - return cons(b)(go(bs)); - })() : []; - return go([...xs]); - }; -``` - - ```applescript -- chop :: ([a] -> (b, [a])) -> [a] -> [b] on chop(f, xs) @@ -30,4 +14,20 @@ on chop(f, xs) end script go's |λ|(xs) end chop +``` + + +```javascript +// chop :: ([a] -> (b, [a])) -> [a] -> [b] +const chop = f => + // A segmentation of xs by tail recursion with a + // function which returns a (prefix, residue) tuple. + xs => { + const go = xs => + 0 < xs.length ? (() => { + const [b, bs] = Array.from(f(xs)); + return cons(b)(go(bs)); + })() : []; + return go([...xs]); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/chr.md b/MD applescript vs javascript/chr.md index 33667628..fd56461a 100644 --- a/MD applescript vs javascript/chr.md +++ b/MD applescript vs javascript/chr.md @@ -1,14 +1,14 @@ -```javascript -// chr :: Int -> Char -const chr = x => - // The character at unix code-point x. - String.fromCodePoint(x); -``` - - ```applescript -- chr :: Int -> Char on chr(n) character id n end chr +``` + + +```javascript +// chr :: Int -> Char +const chr = x => + // The character at unix code-point x. + String.fromCodePoint(x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/chunksOf.md b/MD applescript vs javascript/chunksOf.md index 72f747d0..18631232 100644 --- a/MD applescript vs javascript/chunksOf.md +++ b/MD applescript vs javascript/chunksOf.md @@ -1,3 +1,8 @@ +```applescript +-- chunksOf :: Int -> [a] -> [[a]] on chunksOf(k, xs) script on go(ys) set ab to splitAt(k, ys) set a to |1| of ab if {} ≠ a then {a} & go(|2| of ab) else a end if end go end script result's go(xs) end chunksOf +``` + + ```javascript // chunksOf :: Int -> [a] -> [[a]] const chunksOf = n => @@ -7,9 +12,4 @@ const chunksOf = n => (a, i) => a.concat([xs.slice(i, (n + i))]), [] ); -``` - - -```applescript --- chunksOf :: Int -> [a] -> [[a]] on chunksOf(k, xs) script on go(ys) set ab to splitAt(k, ys) set a to |1| of ab if {} ≠ a then {a} & go(|2| of ab) else a end if end go end script result's go(xs) end chunksOf ``` \ No newline at end of file diff --git a/MD applescript vs javascript/combine.md b/MD applescript vs javascript/combine.md index a3c47b89..f6e5d1d0 100644 --- a/MD applescript vs javascript/combine.md +++ b/MD applescript vs javascript/combine.md @@ -1,19 +1,3 @@ -```javascript -// combine () :: FilePath -> FilePath -> FilePath -const combine = fp => - // Two paths combined with a path separator. - // Just the second path if that starts - // with a path separator. - fp1 => Boolean(fp) && Boolean(fp1) ? ( - '/' === fp1.slice(0, 1) ? ( - fp1 - ) : '/' === fp.slice(-1) ? ( - fp + fp1 - ) : fp + '/' + fp1 - ) : fp + fp1; -``` - - ```applescript -- combine () :: FilePath -> FilePath -> FilePath on combine(fp, fp1) @@ -29,4 +13,20 @@ on combine(fp, fp1) fp & "/" & fp1 end if end combine +``` + + +```javascript +// combine () :: FilePath -> FilePath -> FilePath +const combine = fp => + // Two paths combined with a path separator. + // Just the second path if that starts + // with a path separator. + fp1 => Boolean(fp) && Boolean(fp1) ? ( + '/' === fp1.slice(0, 1) ? ( + fp1 + ) : '/' === fp.slice(-1) ? ( + fp + fp1 + ) : fp + '/' + fp1 + ) : fp + fp1; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/compare.md b/MD applescript vs javascript/compare.md index e7e1b22d..411f3665 100644 --- a/MD applescript vs javascript/compare.md +++ b/MD applescript vs javascript/compare.md @@ -1,10 +1,3 @@ -```javascript -// compare :: a -> a -> Ordering -const compare = a => - b => a < b ? -1 : (a > b ? 1 : 0); -``` - - ```applescript -- compare :: a -> a -> Ordering on compare(a, b) @@ -16,4 +9,11 @@ on compare(a, b) 0 end if end compare +``` + + +```javascript +// compare :: a -> a -> Ordering +const compare = a => + b => a < b ? -1 : (a > b ? 1 : 0); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/comparing.md b/MD applescript vs javascript/comparing.md index 7d2ca7f6..483d9f5e 100644 --- a/MD applescript vs javascript/comparing.md +++ b/MD applescript vs javascript/comparing.md @@ -1,15 +1,3 @@ -```javascript -// comparing :: (a -> b) -> (a -> a -> Ordering) -const comparing = f => - x => y => { - const - a = f(x), - b = f(y); - return a < b ? -1 : (a > b ? 1 : 0); - }; -``` - - ```applescript -- comparing :: (a -> b) -> (a -> a -> Ordering) on comparing(f) @@ -29,4 +17,16 @@ on comparing(f) end |λ| end script end comparing +``` + + +```javascript +// comparing :: (a -> b) -> (a -> a -> Ordering) +const comparing = f => + x => y => { + const + a = f(x), + b = f(y); + return a < b ? -1 : (a > b ? 1 : 0); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/compose (<<<).md b/MD applescript vs javascript/compose (<<<).md index 01cd547e..e1047ff2 100644 --- a/MD applescript vs javascript/compose (<<<).md +++ b/MD applescript vs javascript/compose (<<<).md @@ -1,15 +1,3 @@ -```javascript -// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c -const compose = (...fs) => - // A function defined by the right-to-left - // composition of all the functions in fs. - fs.reduce( - (f, g) => x => f(g(x)), - x => x - ); -``` - - ```applescript -- compose (<<<) :: (b -> c) -> (a -> b) -> a -> c on compose(f, g) @@ -21,4 +9,16 @@ on compose(f, g) end |λ| end script end compose +``` + + +```javascript +// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c +const compose = (...fs) => + // A function defined by the right-to-left + // composition of all the functions in fs. + fs.reduce( + (f, g) => x => f(g(x)), + x => x + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/composeList.md b/MD applescript vs javascript/composeList.md index 6f4c60b9..ea3c5a2e 100644 --- a/MD applescript vs javascript/composeList.md +++ b/MD applescript vs javascript/composeList.md @@ -1,13 +1,3 @@ -```javascript -// composeList :: [(a -> a)] -> (a -> a) -const composeList = fs => - fs.reduce( - (f, g) => x => f(g(x)), - x => x - ); -``` - - ```applescript -- composeList :: [(a -> a)] -> (a -> a) on composeList(fs) @@ -22,4 +12,14 @@ on composeList(fs) end |λ| end script end composeList +``` + + +```javascript +// composeList :: [(a -> a)] -> (a -> a) +const composeList = fs => + fs.reduce( + (f, g) => x => f(g(x)), + x => x + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/composeListR.md b/MD applescript vs javascript/composeListR.md index 1fa4e6b4..ec653148 100644 --- a/MD applescript vs javascript/composeListR.md +++ b/MD applescript vs javascript/composeListR.md @@ -1,10 +1,3 @@ -```javascript -// composeListR :: [(a -> a)] -> (a -> a) -const composeListR = fs => - x => fs.reduce((a, f) => f(a), x); -``` - - ```applescript -- composeListR :: [(a -> a)] -> (a -> a) on composeListR(fs) @@ -20,4 +13,11 @@ on composeListR(fs) end |λ| end script end composeListLR +``` + + +```javascript +// composeListR :: [(a -> a)] -> (a -> a) +const composeListR = fs => + x => fs.reduce((a, f) => f(a), x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/composeR (>>>).md b/MD applescript vs javascript/composeR (>>>).md index 5230402f..dac0c532 100644 --- a/MD applescript vs javascript/composeR (>>>).md +++ b/MD applescript vs javascript/composeR (>>>).md @@ -1,10 +1,3 @@ -```javascript -// composeR (>>>) :: (a -> b) -> (b -> c) -> a -> c -const composeR = f => - g => x => f(g(x)); -``` - - ```applescript -- composeR (>>>) :: (a -> b) -> (b -> c) -> a -> c on composeR(f, g) @@ -14,4 +7,11 @@ on composeR(f, g) end |λ| end script end composeR +``` + + +```javascript +// composeR (>>>) :: (a -> b) -> (b -> c) -> a -> c +const composeR = f => + g => x => f(g(x)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/concat.md b/MD applescript vs javascript/concat.md index 8ed65ef6..ec5308a2 100644 --- a/MD applescript vs javascript/concat.md +++ b/MD applescript vs javascript/concat.md @@ -1,16 +1,3 @@ -```javascript -// concat :: [[a]] -> [a] -// concat :: [String] -> String -const concat = xs => ( - ys => 0 < ys.length ? ( - ys.every(Array.isArray) ? ( - [] - ) : '' - ).concat(...ys) : ys -)(list(xs)); -``` - - ```applescript -- concat :: [[a]] -> [a] -- concat :: [String] -> String @@ -26,4 +13,17 @@ on concat(xs) end repeat acc end concat +``` + + +```javascript +// concat :: [[a]] -> [a] +// concat :: [String] -> String +const concat = xs => ( + ys => 0 < ys.length ? ( + ys.every(Array.isArray) ? ( + [] + ) : '' + ).concat(...ys) : ys +)(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/concatMap.md b/MD applescript vs javascript/concatMap.md index 769c2cff..f8e93670 100644 --- a/MD applescript vs javascript/concatMap.md +++ b/MD applescript vs javascript/concatMap.md @@ -1,3 +1,22 @@ +```applescript +-- concatMap :: (a -> [b]) -> [a] -> [b] +on concatMap(f, xs) + set lng to length of xs + set acc to {} + tell mReturn(f) + repeat with i from 1 to lng + set acc to acc & (|λ|(item i of xs, i, xs)) + end repeat + end tell + if {text, string} contains class of xs then + acc as text + else + acc + end if +end concatMap +``` + + ```javascript // concatMap :: (a -> [b]) -> [a] -> [b] const concatMap = f => @@ -15,23 +34,4 @@ const concatMap = f => ) : '' ).concat(...ys) : ys; }; -``` - - -```applescript --- concatMap :: (a -> [b]) -> [a] -> [b] -on concatMap(f, xs) - set lng to length of xs - set acc to {} - tell mReturn(f) - repeat with i from 1 to lng - set acc to acc & (|λ|(item i of xs, i, xs)) - end repeat - end tell - if {text, string} contains class of xs then - acc as text - else - acc - end if -end concatMap ``` \ No newline at end of file diff --git a/MD applescript vs javascript/cons.md b/MD applescript vs javascript/cons.md index 57d8d504..a793aafb 100644 --- a/MD applescript vs javascript/cons.md +++ b/MD applescript vs javascript/cons.md @@ -1,26 +1,3 @@ -```javascript -// cons :: a -> [a] -> [a] -const cons = x => - // A list constructed from the item x, - // followed by the existing list xs. - xs => Array.isArray(xs) ? ( - [x].concat(xs) - ) : 'GeneratorFunction' !== xs - .constructor.constructor.name ? ( - x + xs - ) : ( // cons(x)(Generator) - function* () { - yield x; - let nxt = xs.next(); - while (!nxt.done) { - yield nxt.value; - nxt = xs.next(); - } - } - )(); -``` - - ```applescript -- cons :: a -> [a] -> [a] on cons(x, xs) @@ -43,4 +20,27 @@ on cons(x, xs) x & xs end if end cons +``` + + +```javascript +// cons :: a -> [a] -> [a] +const cons = x => + // A list constructed from the item x, + // followed by the existing list xs. + xs => Array.isArray(xs) ? ( + [x].concat(xs) + ) : 'GeneratorFunction' !== xs + .constructor.constructor.name ? ( + x + xs + ) : ( // cons(x)(Generator) + function* () { + yield x; + let nxt = xs.next(); + while (!nxt.done) { + yield nxt.value; + nxt = xs.next(); + } + } + )(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/constant.md b/MD applescript vs javascript/constant.md index b5eb6907..e42f9d54 100644 --- a/MD applescript vs javascript/constant.md +++ b/MD applescript vs javascript/constant.md @@ -1,10 +1,3 @@ -```javascript -// constant :: a -> b -> a -const constant = k => - _ => k; -``` - - ```applescript -- constant :: a -> b -> a on |constant|(k) @@ -14,4 +7,11 @@ on |constant|(k) end |λ| end script end |constant| +``` + + +```javascript +// constant :: a -> b -> a +const constant = k => + _ => k; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/createDirectoryIfMissingLR.md b/MD applescript vs javascript/createDirectoryIfMissingLR.md index 086572bc..db65c29d 100644 --- a/MD applescript vs javascript/createDirectoryIfMissingLR.md +++ b/MD applescript vs javascript/createDirectoryIfMissingLR.md @@ -1,3 +1,27 @@ +```applescript +-- createDirectoryIfMissingLR :: Bool -> FilePath -> Either String FilePath +on createDirectoryIfMissingLR(blnParents, fp) + if doesPathExist(fp) then + |Right|(fp) + else + set e to reference + set ca to current application + set oPath to (ca's NSString's stringWithString:(fp))'s ¬ + stringByStandardizingPath + set {blnOK, e} to ca's NSFileManager's ¬ + defaultManager's createDirectoryAtPath:(oPath) ¬ + withIntermediateDirectories:(blnParents) ¬ + attributes:(missing value) |error|:(e) + if blnOK then + |Right|(fp) + else + |Left|((localizedDescription of e) as string) + end if + end if +end createDirectoryIfMissingLR +``` + + ```javascript // createDirectoryIfMissingLR :: Bool -> FilePath -> // Either String FilePath @@ -20,28 +44,4 @@ const createDirectoryIfMissingLR = blnParents => ) : Left(e.localizedDescription); })(); }; -``` - - -```applescript --- createDirectoryIfMissingLR :: Bool -> FilePath -> Either String FilePath -on createDirectoryIfMissingLR(blnParents, fp) - if doesPathExist(fp) then - |Right|(fp) - else - set e to reference - set ca to current application - set oPath to (ca's NSString's stringWithString:(fp))'s ¬ - stringByStandardizingPath - set {blnOK, e} to ca's NSFileManager's ¬ - defaultManager's createDirectoryAtPath:(oPath) ¬ - withIntermediateDirectories:(blnParents) ¬ - attributes:(missing value) |error|:(e) - if blnOK then - |Right|(fp) - else - |Left|((localizedDescription of e) as string) - end if - end if -end createDirectoryIfMissingLR ``` \ No newline at end of file diff --git a/MD applescript vs javascript/curry.md b/MD applescript vs javascript/curry.md index 6ff34102..f6e32438 100644 --- a/MD applescript vs javascript/curry.md +++ b/MD applescript vs javascript/curry.md @@ -1,10 +1,3 @@ -```javascript -// curry :: ((a, b) -> c) -> a -> b -> c -const curry = f => - a => b => f(a, b); -``` - - ```applescript -- curry :: ((a, b) -> c) -> a -> b -> c on curry(f) @@ -18,4 +11,11 @@ on curry(f) end |λ| end script end curry +``` + + +```javascript +// curry :: ((a, b) -> c) -> a -> b -> c +const curry = f => + a => b => f(a, b); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/cycle.md b/MD applescript vs javascript/cycle.md index 4c4260a9..62c82f31 100644 --- a/MD applescript vs javascript/cycle.md +++ b/MD applescript vs javascript/cycle.md @@ -1,16 +1,3 @@ -```javascript -// cycle :: [a] -> Generator [a] -function* cycle(xs) { - const lng = xs.length; - let i = 0; - while (true) { - yield(xs[i]); - i = (1 + i) % lng; - } -} -``` - - ```applescript -- cycle :: [a] -> Generator [a] on cycle(xs) @@ -32,4 +19,17 @@ on cycle(xs) end |λ| end script end cycle +``` + + +```javascript +// cycle :: [a] -> Generator [a] +function* cycle(xs) { + const lng = xs.length; + let i = 0; + while (true) { + yield(xs[i]); + i = (1 + i) % lng; + } +} ``` \ No newline at end of file diff --git a/MD applescript vs javascript/decodedPath.md b/MD applescript vs javascript/decodedPath.md index 00705e38..008e2e54 100644 --- a/MD applescript vs javascript/decodedPath.md +++ b/MD applescript vs javascript/decodedPath.md @@ -1,9 +1,3 @@ -```javascript -// decodedPath :: Percent Encoded String -> FilePath -const decodedPath = decodeURI; -``` - - ```applescript -- use framework "Foundation" -- decodedPath :: Percent Encoded String -> FilePath @@ -13,4 +7,10 @@ on decodedPath(fp) of stringWithString_(fp) ¬ of its NSString) as string end decodedPath +``` + + +```javascript +// decodedPath :: Percent Encoded String -> FilePath +const decodedPath = decodeURI; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/degrees.md b/MD applescript vs javascript/degrees.md index f7387a3f..30cd7ac8 100644 --- a/MD applescript vs javascript/degrees.md +++ b/MD applescript vs javascript/degrees.md @@ -1,13 +1,13 @@ -```javascript -// degrees :: Float x => Radians x -> Degrees x -const degrees = r => - (180 / Math.PI) * r; -``` - - ```applescript -- degrees :: Float x => Radians x -> Degrees x on degrees(r) (180 / pi) * r end degrees +``` + + +```javascript +// degrees :: Float x => Radians x -> Degrees x +const degrees = r => + (180 / Math.PI) * r; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/delete.md b/MD applescript vs javascript/delete.md index 89bfb86d..33e0bf35 100644 --- a/MD applescript vs javascript/delete.md +++ b/MD applescript vs javascript/delete.md @@ -1,18 +1,3 @@ -```javascript -// delete :: Eq a => a -> [a] -> [a] -const delete_ = x => { - // xs with first instance of x (if any) removed. - const go = xs => - 0 < xs.length ? ( - (x === xs[0]) ? ( - xs.slice(1) - ) : [xs[0]].concat(go(xs.slice(1))) - ) : []; - return go; -}; -``` - - ```applescript -- delete :: Eq a => a -> [a] -> [a] on |delete|(x, xs) @@ -36,4 +21,19 @@ on |delete|(x, xs) end if end if end |delete| +``` + + +```javascript +// delete :: Eq a => a -> [a] -> [a] +const delete_ = x => { + // xs with first instance of x (if any) removed. + const go = xs => + 0 < xs.length ? ( + (x === xs[0]) ? ( + xs.slice(1) + ) : [xs[0]].concat(go(xs.slice(1))) + ) : []; + return go; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/deleteAt.md b/MD applescript vs javascript/deleteAt.md index 90b1a856..5ba214fc 100644 --- a/MD applescript vs javascript/deleteAt.md +++ b/MD applescript vs javascript/deleteAt.md @@ -1,13 +1,3 @@ -```javascript -// deleteAt :: Int -> [a] -> [a] -const deleteAt = i => - xs => i <= xs.length ? (() => { - const lr = splitAt(i)(xs); - return lr[0].concat(lr[1].slice(1)); - })() : xs; -``` - - ```applescript -- deleteAt :: Int -> [a] -> [a] on deleteAt(i, xs) @@ -19,4 +9,14 @@ on deleteAt(i, xs) l end if end deleteAt +``` + + +```javascript +// deleteAt :: Int -> [a] -> [a] +const deleteAt = i => + xs => i <= xs.length ? (() => { + const lr = splitAt(i)(xs); + return lr[0].concat(lr[1].slice(1)); + })() : xs; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/deleteBy.md b/MD applescript vs javascript/deleteBy.md index 75ad671a..5afee097 100644 --- a/MD applescript vs javascript/deleteBy.md +++ b/MD applescript vs javascript/deleteBy.md @@ -1,17 +1,3 @@ -```javascript -// deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a] -const deleteBy = fEq => - x => { - const go = xs => 0 < xs.length ? ( - fEq(x)(xs[0]) ? ( - xs.slice(1) - ) : [xs[0]].concat(go(xs.slice(1))) - ) : []; - return go; - }; -``` - - ```applescript -- deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a] on deleteBy(fnEq, x, xs) @@ -32,4 +18,18 @@ on deleteBy(fnEq, x, xs) end script go's |λ|(xs) end deleteBy +``` + + +```javascript +// deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a] +const deleteBy = fEq => + x => { + const go = xs => 0 < xs.length ? ( + fEq(x)(xs[0]) ? ( + xs.slice(1) + ) : [xs[0]].concat(go(xs.slice(1))) + ) : []; + return go; + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/deleteFirst.md b/MD applescript vs javascript/deleteFirst.md index 9257aecc..61382010 100644 --- a/MD applescript vs javascript/deleteFirst.md +++ b/MD applescript vs javascript/deleteFirst.md @@ -1,16 +1,3 @@ -```javascript -// deleteFirst :: a -> [a] -> [a] -const deleteFirst = x => { - const go = xs => 0 < xs.length ? ( - x === xs[0] ? ( - xs.slice(1) - ) : [xs[0]].concat(go(xs.slice(1))) - ) : []; - return go; -}; -``` - - ```applescript -- deleteFirst :: a -> [a] -> [a] on deleteFirst(x, xs) @@ -30,4 +17,17 @@ on deleteFirst(x, xs) end script go's |λ|(xs) end deleteFirst +``` + + +```javascript +// deleteFirst :: a -> [a] -> [a] +const deleteFirst = x => { + const go = xs => 0 < xs.length ? ( + x === xs[0] ? ( + xs.slice(1) + ) : [xs[0]].concat(go(xs.slice(1))) + ) : []; + return go; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/deleteFirstsBy.md b/MD applescript vs javascript/deleteFirstsBy.md index 92a827c9..9c9bec9a 100644 --- a/MD applescript vs javascript/deleteFirstsBy.md +++ b/MD applescript vs javascript/deleteFirstsBy.md @@ -1,12 +1,3 @@ -```javascript -// deleteFirstsBy :: (a -> a -> Bool) -> [a] -> [a] -> [a] -const deleteFirstsBy = fEq => - // The first list purged of the first instance of - // each predicate-matching element in the second list. - foldl(flip(deleteBy(fEq))); -``` - - ```applescript -- deleteFirstsBy :: (a -> a -> Bool) -> [a] -> [a] -> [a] on deleteFirstsBy(fnEq, xs, ys) @@ -17,4 +8,13 @@ on deleteFirstsBy(fnEq, xs, ys) end script foldl(result, xs, ys) end deleteFirstsBy +``` + + +```javascript +// deleteFirstsBy :: (a -> a -> Bool) -> [a] -> [a] -> [a] +const deleteFirstsBy = fEq => + // The first list purged of the first instance of + // each predicate-matching element in the second list. + foldl(flip(deleteBy(fEq))); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/deleteKey.md b/MD applescript vs javascript/deleteKey.md index 91f2911e..0a269c52 100644 --- a/MD applescript vs javascript/deleteKey.md +++ b/MD applescript vs javascript/deleteKey.md @@ -1,14 +1,3 @@ -```javascript -// deleteKey :: String -> Dict -> Dict -const deleteKey = k => - // A new dictionary, without the key k. - dct => { - const dct2 = Object.assign({}, dct); - return (delete dct2[k], dct2); - }; -``` - - ```applescript -- deleteKey :: String -> Dict -> Dict on deleteKey(k, rec) @@ -17,4 +6,15 @@ on deleteKey(k, rec) removeObjectForKey_(k) of nsDct nsDct as record end deleteKey +``` + + +```javascript +// deleteKey :: String -> Dict -> Dict +const deleteKey = k => + // A new dictionary, without the key k. + dct => { + const dct2 = Object.assign({}, dct); + return (delete dct2[k], dct2); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/dictFromList.md b/MD applescript vs javascript/dictFromList.md index d85abe74..c2c893d7 100644 --- a/MD applescript vs javascript/dictFromList.md +++ b/MD applescript vs javascript/dictFromList.md @@ -1,10 +1,3 @@ -```javascript -// dictFromList :: [(k, v)] -> Dict -const dictFromList = kvs => - Object.fromEntries(kvs); -``` - - ```applescript -- dictFromList :: [(k, v)] -> Dict on dictFromList(kvs) @@ -19,4 +12,11 @@ on dictFromList(kvs) forKeys:(my map(go, my fst(tpl))))) as record end tell end dictFromList +``` + + +```javascript +// dictFromList :: [(k, v)] -> Dict +const dictFromList = kvs => + Object.fromEntries(kvs); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/difference.md b/MD applescript vs javascript/difference.md index 0c6d0d2b..c20ba3df 100644 --- a/MD applescript vs javascript/difference.md +++ b/MD applescript vs javascript/difference.md @@ -1,13 +1,3 @@ -```javascript -// difference :: Eq a => [a] -> [a] -> [a] -const difference = xs => - ys => { - const s = new Set(ys); - return xs.filter(x => !s.has(x)); - }; -``` - - ```applescript -- difference :: Eq a => [a] -> [a] -> [a] on difference(xs, ys) @@ -18,4 +8,14 @@ on difference(xs, ys) end script filter(p, xs) end difference +``` + + +```javascript +// difference :: Eq a => [a] -> [a] -> [a] +const difference = xs => + ys => { + const s = new Set(ys); + return xs.filter(x => !s.has(x)); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/differenceGen.md b/MD applescript vs javascript/differenceGen.md index cf9fd4a4..225713cc 100644 --- a/MD applescript vs javascript/differenceGen.md +++ b/MD applescript vs javascript/differenceGen.md @@ -1,24 +1,3 @@ -```javascript -// differenceGen :: Gen [a] -> Gen [a] -> Gen [a] -const differenceGen = ga => { - return function*(gb) { - // All values of generator stream ga except any - // already seen in generator stream gb. - const - stream = zipGen(ga)(gb), - sb = new Set([]); - let xy = take(1)(stream); - while (0 < xy.length) { - const [x, y] = Array.from(xy[0]); - sb.add(y); - if (!sb.has(x)) yield x; - xy = take(1)(stream); - } - }; -}; -``` - - ```applescript -- differenceGen :: Gen [a] -> Gen [a] -> Gen [a] on differenceGen(ga, gb) @@ -45,4 +24,25 @@ on differenceGen(ga, gb) end |λ| end script end differenceGen +``` + + +```javascript +// differenceGen :: Gen [a] -> Gen [a] -> Gen [a] +const differenceGen = ga => { + return function*(gb) { + // All values of generator stream ga except any + // already seen in generator stream gb. + const + stream = zipGen(ga)(gb), + sb = new Set([]); + let xy = take(1)(stream); + while (0 < xy.length) { + const [x, y] = Array.from(xy[0]); + sb.add(y); + if (!sb.has(x)) yield x; + xy = take(1)(stream); + } + }; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/digitToInt.md b/MD applescript vs javascript/digitToInt.md index bcab1a8b..cea75436 100644 --- a/MD applescript vs javascript/digitToInt.md +++ b/MD applescript vs javascript/digitToInt.md @@ -1,3 +1,27 @@ +```applescript +-- digitToInt :: Char -> Int +on digitToInt(c) + set oc to id of c + if 48 > oc or 102 < oc then + missing value + else + set dec to oc - (id of "0") + set hexu to oc - (id of "A") + set hexl to oc - (id of "a") + if 9 ≥ dec then + dec + else if 0 ≤ hexu and 5 ≥ hexu then + 10 + hexu + else if 0 ≤ hexl and 5 ≥ hexl then + 10 + hexl + else + missing value + end if + end if +end digitToInt +``` + + ```javascript // digitToInt :: Char -> Int const digitToInt = c => { @@ -20,28 +44,4 @@ const digitToInt = c => { ) : undefined; })(); }; -``` - - -```applescript --- digitToInt :: Char -> Int -on digitToInt(c) - set oc to id of c - if 48 > oc or 102 < oc then - missing value - else - set dec to oc - (id of "0") - set hexu to oc - (id of "A") - set hexl to oc - (id of "a") - if 9 ≥ dec then - dec - else if 0 ≤ hexu and 5 ≥ hexu then - 10 + hexu - else if 0 ≤ hexl and 5 ≥ hexl then - 10 + hexl - else - missing value - end if - end if -end digitToInt ``` \ No newline at end of file diff --git a/MD applescript vs javascript/div.md b/MD applescript vs javascript/div.md index 7aa66c8d..fee82129 100644 --- a/MD applescript vs javascript/div.md +++ b/MD applescript vs javascript/div.md @@ -1,10 +1,3 @@ -```javascript -// div :: Int -> Int -> Int -const div = x => - y => Math.floor(x / y); -``` - - ```applescript -- div :: Int -> Int -> Int on |div|(a, b) @@ -16,4 +9,11 @@ on |div|(a, b) i end if end |div| +``` + + +```javascript +// div :: Int -> Int -> Int +const div = x => + y => Math.floor(x / y); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/divMod.md b/MD applescript vs javascript/divMod.md index bbf87ee1..7b7af4ce 100644 --- a/MD applescript vs javascript/divMod.md +++ b/MD applescript vs javascript/divMod.md @@ -1,17 +1,3 @@ -```javascript -// divMod :: Int -> Int -> (Int, Int) -const divMod = n => d => { - // Integer division, truncated toward negative infinity, - // and integer modulus such that: - // (x `div` y)*y + (x `mod` y) == x - const [q, r] = [Math.trunc(n / d), n % d]; - return signum(n) === signum(-d) ? ( - Tuple(q - 1)(r + d) - ) : Tuple(q)(r); -}; -``` - - ```applescript -- divMod :: Int -> Int -> (Int, Int) on divMod(n, d) @@ -25,4 +11,18 @@ on divMod(n, d) {q, r} end if end divMod +``` + + +```javascript +// divMod :: Int -> Int -> (Int, Int) +const divMod = n => d => { + // Integer division, truncated toward negative infinity, + // and integer modulus such that: + // (x `div` y)*y + (x `mod` y) == x + const [q, r] = [Math.trunc(n / d), n % d]; + return signum(n) === signum(-d) ? ( + Tuple(q - 1)(r + d) + ) : Tuple(q)(r); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/doesDirectoryExist.md b/MD applescript vs javascript/doesDirectoryExist.md index a1132bb5..9ce8606c 100644 --- a/MD applescript vs javascript/doesDirectoryExist.md +++ b/MD applescript vs javascript/doesDirectoryExist.md @@ -1,3 +1,8 @@ +```applescript +-- doesDirectoryExist :: FilePath -> IO Bool on doesDirectoryExist(strPath) set ca to current application set oPath to (ca's NSString's stringWithString:strPath)'s ¬ stringByStandardizingPath set {bln, v} to (ca's NSFileManager's defaultManager's ¬ fileExistsAtPath:oPath isDirectory:(reference)) bln and v end doesDirectoryExist +``` + + ```javascript // doesDirectoryExist :: FilePath -> IO Bool const doesDirectoryExist = fp => { @@ -8,9 +13,4 @@ const doesDirectoryExist = fp => { .stringByStandardizingPath, ref ) && ref[0]; }; -``` - - -```applescript --- doesDirectoryExist :: FilePath -> IO Bool on doesDirectoryExist(strPath) set ca to current application set oPath to (ca's NSString's stringWithString:strPath)'s ¬ stringByStandardizingPath set {bln, v} to (ca's NSFileManager's defaultManager's ¬ fileExistsAtPath:oPath isDirectory:(reference)) bln and v end doesDirectoryExist ``` \ No newline at end of file diff --git a/MD applescript vs javascript/doesFileExist.md b/MD applescript vs javascript/doesFileExist.md index 39c54c40..18a9d95f 100644 --- a/MD applescript vs javascript/doesFileExist.md +++ b/MD applescript vs javascript/doesFileExist.md @@ -1,16 +1,3 @@ -```javascript -// doesFileExist :: FilePath -> IO Bool -const doesFileExist = fp => { - const ref = Ref(); - return $.NSFileManager.defaultManager - .fileExistsAtPathIsDirectory( - $(fp) - .stringByStandardizingPath, ref - ) && 1 !== ref[0]; -}; -``` - - ```applescript -- doesFileExist :: FilePath -> IO Bool on doesFileExist(strPath) @@ -21,4 +8,17 @@ on doesFileExist(strPath) fileExistsAtPath:oPath isDirectory:(reference)) bln and (int ≠ 1) end doesFileExist +``` + + +```javascript +// doesFileExist :: FilePath -> IO Bool +const doesFileExist = fp => { + const ref = Ref(); + return $.NSFileManager.defaultManager + .fileExistsAtPathIsDirectory( + $(fp) + .stringByStandardizingPath, ref + ) && 1 !== ref[0]; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/doesPathExist.md b/MD applescript vs javascript/doesPathExist.md index e845bcec..1e1bffba 100644 --- a/MD applescript vs javascript/doesPathExist.md +++ b/MD applescript vs javascript/doesPathExist.md @@ -1,13 +1,3 @@ -```javascript -// doesPathExist :: FilePath -> IO Bool -const doesPathExist = fp => - $.NSFileManager.defaultManager - .fileExistsAtPath( - $(fp).stringByStandardizingPath - ); -``` - - ```applescript -- doesPathExist :: FilePath -> IO Bool on doesPathExist(strPath) @@ -17,4 +7,14 @@ on doesPathExist(strPath) stringWithString:strPath)'s ¬ stringByStandardizingPath) end doesPathExist +``` + + +```javascript +// doesPathExist :: FilePath -> IO Bool +const doesPathExist = fp => + $.NSFileManager.defaultManager + .fileExistsAtPath( + $(fp).stringByStandardizingPath + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/draw.md b/MD applescript vs javascript/draw.md index ab07c994..43825b20 100644 --- a/MD applescript vs javascript/draw.md +++ b/MD applescript vs javascript/draw.md @@ -1,37 +1,3 @@ -```javascript -// draw :: Tree String -> [String] -const draw = node => { - // shift :: String -> String -> [String] -> [String] - const shifted = (first, other, xs) => - zipWithList(append)( - cons(first)( - replicate(xs.length - 1)( - other - ) - ) - )(xs); - // drawSubTrees :: [Tree String] -> [String] - const drawSubTrees = xs => { - const lng = xs.length; - return 0 < lng ? ( - 1 < lng ? append( - cons('│')( - shifted('├─ ', '│ ', draw(xs[0])) - ) - )( - drawSubTrees(xs.slice(1)) - ) : cons('│')( - shifted('└─ ', ' ', draw(xs[0])) - ) - ) : []; - }; - return append(lines(node.root))( - drawSubTrees(node.nest) - ); -}; -``` - - ```applescript -- draw :: Tree String -> [String] on draw(tree) @@ -63,4 +29,38 @@ on draw(tree) paragraphs of (root of tree) & |λ|(nest of tree) of drawSubTrees end draw +``` + + +```javascript +// draw :: Tree String -> [String] +const draw = node => { + // shift :: String -> String -> [String] -> [String] + const shifted = (first, other, xs) => + zipWithList(append)( + cons(first)( + replicate(xs.length - 1)( + other + ) + ) + )(xs); + // drawSubTrees :: [Tree String] -> [String] + const drawSubTrees = xs => { + const lng = xs.length; + return 0 < lng ? ( + 1 < lng ? append( + cons('│')( + shifted('├─ ', '│ ', draw(xs[0])) + ) + )( + drawSubTrees(xs.slice(1)) + ) : cons('│')( + shifted('└─ ', ' ', draw(xs[0])) + ) + ) : []; + }; + return append(lines(node.root))( + drawSubTrees(node.nest) + ); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/drawForest.md b/MD applescript vs javascript/drawForest.md index ad13deb6..ba4210be 100644 --- a/MD applescript vs javascript/drawForest.md +++ b/MD applescript vs javascript/drawForest.md @@ -1,13 +1,13 @@ -```javascript -// drawForest :: [Tree String] -> String -const drawForest = trees => - trees.map(drawTree).join('\n'); -``` - - ```applescript -- drawForest :: [Tree String] -> String on drawForest(trees) intercalate("\n\n", map(my drawTree, trees)) end drawForest +``` + + +```javascript +// drawForest :: [Tree String] -> String +const drawForest = trees => + trees.map(drawTree).join('\n'); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/drawTree.md b/MD applescript vs javascript/drawTree.md index 5e037a1f..eb2ce3c8 100644 --- a/MD applescript vs javascript/drawTree.md +++ b/MD applescript vs javascript/drawTree.md @@ -1,13 +1,13 @@ -```javascript -// drawTree :: Tree String -> String -const drawTree = tree => - unlines(draw(tree)); -``` - - ```applescript -- drawTree :: Tree String -> String on drawTree(tree) unlines(draw(tree)) end drawTree +``` + + +```javascript +// drawTree :: Tree String -> String +const drawTree = tree => + unlines(draw(tree)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/drawTree2.md b/MD applescript vs javascript/drawTree2.md index 3c46d435..9b97d0b1 100644 --- a/MD applescript vs javascript/drawTree2.md +++ b/MD applescript vs javascript/drawTree2.md @@ -1,105 +1,3 @@ -```javascript -// drawTree2 :: Bool -> Bool -> Tree String -> String -const drawTree2 = blnCompact => blnPruned => tree => { - // Tree design and algorithm inspired by the Haskell snippet at: - // https://doisinkidney.com/snippets/drawing-trees.html - const - // Lefts, Middle, Rights - lmrFromStrings = xs => { - const [ls, rs] = Array.from(splitAt( - Math.floor(xs.length / 2) - )(xs)); - return TupleN(ls, rs[0], rs.slice(1)); - }, - stringsFromLMR = lmr => - Array.from(lmr).reduce((a, x) => a.concat(x), []), - fghOverLMR = (f, g, h) => lmr => { - const [ls, m, rs] = Array.from(lmr); - return TupleN(ls.map(f), g(m), rs.map(h)); - }; - const lmrBuild = (f, w) => wsTree => { - const - leftPad = n => s => ' '.repeat(n) + s, - xs = wsTree.nest, - lng = xs.length, - [nChars, x] = Array.from(wsTree.root); - - // LEAF NODE -------------------------------------- - return 0 === lng ? ( - TupleN([], '─'.repeat(w - nChars) + x, []) - - // NODE WITH SINGLE CHILD ------------------------- - ) : 1 === lng ? (() => { - const indented = leftPad(1 + w); - return fghOverLMR( - indented, - z => '─'.repeat(w - nChars) + x + '─' + z, - indented - )(f(xs[0])); - - // NODE WITH CHILDREN ----------------------------- - })() : (() => { - const - cFix = x => xs => x + xs, - treeFix = (l, m, r) => compose( - stringsFromLMR, - fghOverLMR(cFix(l), cFix(m), cFix(r)) - ), - _x = '─'.repeat(w - nChars) + x, - indented = leftPad(w), - lmrs = xs.map(f); - return fghOverLMR( - indented, - s => _x + ({ - '┌': '┬', - '├': '┼', - '│': '┤', - '└': '┴' - })[s[0]] + s.slice(1), - indented - )(lmrFromStrings( - intercalate( - blnCompact ? [] : ['│'] - )( - [treeFix(' ', '┌', '│')(lmrs[0])] - .concat(init(lmrs.slice(1)).map( - treeFix('│', '├', '│') - )) - .concat([treeFix('│', '└', ' ')( - lmrs[lmrs.length - 1] - )]) - ) - )); - })(); - }; - const - measuredTree = fmapTree( - v => { - const s = ' ' + v + ' '; - return Tuple(s.length)(s); - })(tree), - levelWidths = levels(measuredTree) - .reduce( - (a, level) => a.concat(maximum(level.map(fst))), - [] - ), - treeLines = stringsFromLMR( - levelWidths.reduceRight( - lmrBuild, x => x - )(measuredTree) - ); - return unlines( - blnPruned ? ( - treeLines.filter( - s => s.split('') - .some(c => !' │'.includes(c)) - ) - ) : treeLines - ); -}; -``` - - ```applescript -- drawTree2 :: Bool -> Bool -> Tree String -> String on drawTree2(blnCompressed, blnPruned, tree) @@ -272,4 +170,106 @@ on drawTree2(blnCompressed, blnPruned, tree) end if unlines(xs) end drawTree2 +``` + + +```javascript +// drawTree2 :: Bool -> Bool -> Tree String -> String +const drawTree2 = blnCompact => blnPruned => tree => { + // Tree design and algorithm inspired by the Haskell snippet at: + // https://doisinkidney.com/snippets/drawing-trees.html + const + // Lefts, Middle, Rights + lmrFromStrings = xs => { + const [ls, rs] = Array.from(splitAt( + Math.floor(xs.length / 2) + )(xs)); + return TupleN(ls, rs[0], rs.slice(1)); + }, + stringsFromLMR = lmr => + Array.from(lmr).reduce((a, x) => a.concat(x), []), + fghOverLMR = (f, g, h) => lmr => { + const [ls, m, rs] = Array.from(lmr); + return TupleN(ls.map(f), g(m), rs.map(h)); + }; + const lmrBuild = (f, w) => wsTree => { + const + leftPad = n => s => ' '.repeat(n) + s, + xs = wsTree.nest, + lng = xs.length, + [nChars, x] = Array.from(wsTree.root); + + // LEAF NODE -------------------------------------- + return 0 === lng ? ( + TupleN([], '─'.repeat(w - nChars) + x, []) + + // NODE WITH SINGLE CHILD ------------------------- + ) : 1 === lng ? (() => { + const indented = leftPad(1 + w); + return fghOverLMR( + indented, + z => '─'.repeat(w - nChars) + x + '─' + z, + indented + )(f(xs[0])); + + // NODE WITH CHILDREN ----------------------------- + })() : (() => { + const + cFix = x => xs => x + xs, + treeFix = (l, m, r) => compose( + stringsFromLMR, + fghOverLMR(cFix(l), cFix(m), cFix(r)) + ), + _x = '─'.repeat(w - nChars) + x, + indented = leftPad(w), + lmrs = xs.map(f); + return fghOverLMR( + indented, + s => _x + ({ + '┌': '┬', + '├': '┼', + '│': '┤', + '└': '┴' + })[s[0]] + s.slice(1), + indented + )(lmrFromStrings( + intercalate( + blnCompact ? [] : ['│'] + )( + [treeFix(' ', '┌', '│')(lmrs[0])] + .concat(init(lmrs.slice(1)).map( + treeFix('│', '├', '│') + )) + .concat([treeFix('│', '└', ' ')( + lmrs[lmrs.length - 1] + )]) + ) + )); + })(); + }; + const + measuredTree = fmapTree( + v => { + const s = ' ' + v + ' '; + return Tuple(s.length)(s); + })(tree), + levelWidths = levels(measuredTree) + .reduce( + (a, level) => a.concat(maximum(level.map(fst))), + [] + ), + treeLines = stringsFromLMR( + levelWidths.reduceRight( + lmrBuild, x => x + )(measuredTree) + ); + return unlines( + blnPruned ? ( + treeLines.filter( + s => s.split('') + .some(c => !' │'.includes(c)) + ) + ) : treeLines + ); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/drop.md b/MD applescript vs javascript/drop.md index 5b9bfb4b..45c9b052 100644 --- a/MD applescript vs javascript/drop.md +++ b/MD applescript vs javascript/drop.md @@ -1,14 +1,3 @@ -```javascript -// drop :: Int -> [a] -> [a] -// drop :: Int -> Generator [a] -> Generator [a] -// drop :: Int -> String -> String -const drop = n => - xs => Infinity > length(xs) ? ( - xs.slice(n) - ) : (take(n)(xs), xs); -``` - - ```applescript -- drop :: Int -> [a] -> [a] -- drop :: Int -> String -> String @@ -33,4 +22,15 @@ on drop(n, xs) return xs end if end drop +``` + + +```javascript +// drop :: Int -> [a] -> [a] +// drop :: Int -> Generator [a] -> Generator [a] +// drop :: Int -> String -> String +const drop = n => + xs => Infinity > length(xs) ? ( + xs.slice(n) + ) : (take(n)(xs), xs); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/dropAround.md b/MD applescript vs javascript/dropAround.md index c0de74c3..011af02f 100644 --- a/MD applescript vs javascript/dropAround.md +++ b/MD applescript vs javascript/dropAround.md @@ -1,3 +1,12 @@ +```applescript +-- dropAround :: (a -> Bool) -> [a] -> [a] +-- dropAround :: (Char -> Bool) -> String -> String +on dropAround(p, xs) + dropWhile(p, dropWhileEnd(p, xs)) +end dropAround +``` + + ```javascript // dropAround :: (a -> Bool) -> [a] -> [a] // dropAround :: (Char -> Bool) -> String -> String @@ -5,13 +14,4 @@ const dropAround = p => xs => dropWhile(p)( dropWhileEnd(p)(xs) ); -``` - - -```applescript --- dropAround :: (a -> Bool) -> [a] -> [a] --- dropAround :: (Char -> Bool) -> String -> String -on dropAround(p, xs) - dropWhile(p, dropWhileEnd(p, xs)) -end dropAround ``` \ No newline at end of file diff --git a/MD applescript vs javascript/dropFileName.md b/MD applescript vs javascript/dropFileName.md index 43044428..05ec54d5 100644 --- a/MD applescript vs javascript/dropFileName.md +++ b/MD applescript vs javascript/dropFileName.md @@ -1,17 +1,3 @@ -```javascript -// dropFileName :: FilePath -> FilePath -const dropFileName = fp => - '' !== fp ? (() => { - const - xs = (fp.split('/')) - .slice(0, -1); - return xs.length > 0 ? ( - xs.join('/') + '/' - ) : './'; - })() : './'; -``` - - ```applescript -- dropFileName :: FilePath -> FilePath on dropFileName(strPath) @@ -30,4 +16,18 @@ on dropFileName(strPath) "./" end if end dropFileName +``` + + +```javascript +// dropFileName :: FilePath -> FilePath +const dropFileName = fp => + '' !== fp ? (() => { + const + xs = (fp.split('/')) + .slice(0, -1); + return xs.length > 0 ? ( + xs.join('/') + '/' + ) : './'; + })() : './'; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/dropLength.md b/MD applescript vs javascript/dropLength.md index 7100c223..551185fc 100644 --- a/MD applescript vs javascript/dropLength.md +++ b/MD applescript vs javascript/dropLength.md @@ -1,18 +1,3 @@ -```javascript -// dropLength :: [a] -> [b] -> [b] -const dropLength = xs => - ys => { - const go = (x, y) => - 0 < x.length ? ( - 0 < y.length ? ( - go(x.slice(1), y.slice(1)) - ) : [] - ) : y; - return go(xs, ys); - }; -``` - - ```applescript -- dropLength :: [a] -> [b] -> [b] on dropLength(xs, ys) @@ -31,4 +16,19 @@ on dropLength(xs, ys) end script go's |λ|(xs, ys) end dropLength +``` + + +```javascript +// dropLength :: [a] -> [b] -> [b] +const dropLength = xs => + ys => { + const go = (x, y) => + 0 < x.length ? ( + 0 < y.length ? ( + go(x.slice(1), y.slice(1)) + ) : [] + ) : y; + return go(xs, ys); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/dropLengthMaybe.md b/MD applescript vs javascript/dropLengthMaybe.md index a91cb389..fc4f8e71 100644 --- a/MD applescript vs javascript/dropLengthMaybe.md +++ b/MD applescript vs javascript/dropLengthMaybe.md @@ -1,18 +1,3 @@ -```javascript -// dropLengthMaybe :: [a] -> [b] -> Maybe [b] -const dropLengthMaybe = xs => - ys => { - const go = (x, y) => - 0 < x.length ? ( - 0 < y.length ? ( - go(x.slice(1), y.slice(1)) - ) : Nothing() - ) : Just(y); - return go(xs, ys); - }; -``` - - ```applescript -- dropLengthMaybe :: [a] -> [b] -> Maybe [b] on dropLengthMaybe(xs, ys) @@ -31,4 +16,19 @@ on dropLengthMaybe(xs, ys) end script go's |λ|(xs, ys) end dropLengthMaybe +``` + + +```javascript +// dropLengthMaybe :: [a] -> [b] -> Maybe [b] +const dropLengthMaybe = xs => + ys => { + const go = (x, y) => + 0 < x.length ? ( + 0 < y.length ? ( + go(x.slice(1), y.slice(1)) + ) : Nothing() + ) : Just(y); + return go(xs, ys); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/dropWhile.md b/MD applescript vs javascript/dropWhile.md index f0f7b01a..cab78088 100644 --- a/MD applescript vs javascript/dropWhile.md +++ b/MD applescript vs javascript/dropWhile.md @@ -1,18 +1,3 @@ -```javascript -// dropWhile :: (a -> Bool) -> [a] -> [a] -// dropWhile :: (Char -> Bool) -> String -> String -const dropWhile = p => - xs => { - const n = xs.length; - return xs.slice( - 0 < n ? until( - i => n === i || !p(xs[i]) - )(i => 1 + i)(0) : 0 - ); - }; -``` - - ```applescript -- dropWhile :: (a -> Bool) -> [a] -> [a] -- dropWhile :: (Char -> Bool) -> String -> String @@ -26,4 +11,19 @@ on dropWhile(p, xs) end tell drop(i - 1, xs) end dropWhile +``` + + +```javascript +// dropWhile :: (a -> Bool) -> [a] -> [a] +// dropWhile :: (Char -> Bool) -> String -> String +const dropWhile = p => + xs => { + const n = xs.length; + return xs.slice( + 0 < n ? until( + i => n === i || !p(xs[i]) + )(i => 1 + i)(0) : 0 + ); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/dropWhileEnd.md b/MD applescript vs javascript/dropWhileEnd.md index 6f9fd8c7..38affdfe 100644 --- a/MD applescript vs javascript/dropWhileEnd.md +++ b/MD applescript vs javascript/dropWhileEnd.md @@ -1,17 +1,3 @@ -```javascript -// dropWhileEnd :: (a -> Bool) -> [a] -> [a] -// dropWhileEnd :: (Char -> Bool) -> String -> [Char] -const dropWhileEnd = p => - // xs without the longest suffix for which - // p returns true for all elements. - xs => { - let i = xs.length; - while (i-- && p(xs[i])) {} - return xs.slice(0, i + 1); - }; -``` - - ```applescript -- dropWhileEnd :: (a -> Bool) -> [a] -> [a] -- dropWhileEnd :: (Char -> Bool) -> String -> String @@ -24,4 +10,18 @@ on dropWhileEnd(p, xs) end tell take(i, xs) end dropWhileEnd +``` + + +```javascript +// dropWhileEnd :: (a -> Bool) -> [a] -> [a] +// dropWhileEnd :: (Char -> Bool) -> String -> [Char] +const dropWhileEnd = p => + // xs without the longest suffix for which + // p returns true for all elements. + xs => { + let i = xs.length; + while (i-- && p(xs[i])) {} + return xs.slice(0, i + 1); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/dropWhileGen.md b/MD applescript vs javascript/dropWhileGen.md index 0cf928a0..09992884 100644 --- a/MD applescript vs javascript/dropWhileGen.md +++ b/MD applescript vs javascript/dropWhileGen.md @@ -1,3 +1,17 @@ +```applescript +-- dropWhileGen :: (a -> Bool) -> Gen [a] -> [a] +on dropWhileGen(p, xs) + set v to |λ|() of xs + tell mReturn(p) + repeat while (|λ|(v)) + set v to xs's |λ|() + end repeat + end tell + return cons(v, xs) +end dropWhileGen +``` + + ```javascript // dropWhileGen :: (a -> Bool) -> Gen [a] -> [a] const dropWhileGen = p => @@ -11,18 +25,4 @@ const dropWhileGen = p => } return cons(v)(xs); }; -``` - - -```applescript --- dropWhileGen :: (a -> Bool) -> Gen [a] -> [a] -on dropWhileGen(p, xs) - set v to |λ|() of xs - tell mReturn(p) - repeat while (|λ|(v)) - set v to xs's |λ|() - end repeat - end tell - return cons(v, xs) -end dropWhileGen ``` \ No newline at end of file diff --git a/MD applescript vs javascript/either.md b/MD applescript vs javascript/either.md index 802363e1..9b094ae4 100644 --- a/MD applescript vs javascript/either.md +++ b/MD applescript vs javascript/either.md @@ -1,3 +1,15 @@ +```applescript +-- either :: (a -> c) -> (b -> c) -> Either a b -> c +on either(lf, rf, e) + if missing value is |Left| of e then + tell mReturn(rf) to |λ|(|Right| of e) + else + tell mReturn(lf) to |λ|(|Left| of e) + end if +end either +``` + + ```javascript // either :: (a -> c) -> (b -> c) -> Either a b -> c const either = fl => @@ -9,16 +21,4 @@ const either = fl => fl(e.Left) ) : fr(e.Right) ) : undefined; -``` - - -```applescript --- either :: (a -> c) -> (b -> c) -> Either a b -> c -on either(lf, rf, e) - if missing value is |Left| of e then - tell mReturn(rf) to |λ|(|Right| of e) - else - tell mReturn(lf) to |λ|(|Left| of e) - end if -end either ``` \ No newline at end of file diff --git a/MD applescript vs javascript/elem.md b/MD applescript vs javascript/elem.md index dad5c487..6d2b0181 100644 --- a/MD applescript vs javascript/elem.md +++ b/MD applescript vs javascript/elem.md @@ -1,3 +1,13 @@ +```applescript +-- elem :: Eq a => a -> [a] -> Bool +on elem(x, xs) + considering case + xs contains x + end considering +end elem +``` + + ```javascript // elem :: Eq a => a -> [a] -> Bool const elem = x => @@ -8,14 +18,4 @@ const elem = x => xs['Set' !== t ? 'includes' : 'has'](x) ) : xs.some(eq(x)); }; -``` - - -```applescript --- elem :: Eq a => a -> [a] -> Bool -on elem(x, xs) - considering case - xs contains x - end considering -end elem ``` \ No newline at end of file diff --git a/MD applescript vs javascript/elemAtMay.md b/MD applescript vs javascript/elemAtMay.md index ff413578..a8389612 100644 --- a/MD applescript vs javascript/elemAtMay.md +++ b/MD applescript vs javascript/elemAtMay.md @@ -1,23 +1,3 @@ -```javascript -// elemAtMay :: Int -> Dict -> Maybe (String, a) -// elemAtMay :: Int -> [a] -> Maybe a -const elemAtMay = i => - // Just the item at the indexed position in an array, - // or in the lexically sorted key-values of a dict, - // or Nothing, if the index is out of range. - x => { - const - bln = Array.isArray(x), - k = bln ? i : Object.keys(x) - .sort()[i], - v = x[k]; - return undefined !== v ? ( - Just(bln ? v : Tuple(k, v)) - ) : Nothing(); - }; -``` - - ```applescript -- If x is a Dictionary then reads the Int as an index -- into the lexically sorted keys of the Dict, @@ -47,4 +27,24 @@ on elemAtMay(i, x) end if end if end elemAtMay +``` + + +```javascript +// elemAtMay :: Int -> Dict -> Maybe (String, a) +// elemAtMay :: Int -> [a] -> Maybe a +const elemAtMay = i => + // Just the item at the indexed position in an array, + // or in the lexically sorted key-values of a dict, + // or Nothing, if the index is out of range. + x => { + const + bln = Array.isArray(x), + k = bln ? i : Object.keys(x) + .sort()[i], + v = x[k]; + return undefined !== v ? ( + Just(bln ? v : Tuple(k, v)) + ) : Nothing(); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/elemIndex.md b/MD applescript vs javascript/elemIndex.md index 00f460e1..3fd2bf96 100644 --- a/MD applescript vs javascript/elemIndex.md +++ b/MD applescript vs javascript/elemIndex.md @@ -1,3 +1,8 @@ +```applescript +-- elemIndex :: Eq a => a -> [a] -> Maybe Int on elemIndex(x, xs) -- Just the zero-based index of x in xs, -- or Nothing if x is not found in xs. set lng to length of xs repeat with i from 1 to lng if x = (item i of xs) then return Just(i - 1) end repeat return Nothing() end elemIndex +``` + + ```javascript // elemIndex :: Eq a => a -> [a] -> Maybe Int const elemIndex = x => @@ -7,9 +12,4 @@ const elemIndex = x => Nothing() ) : Just(i); }; -``` - - -```applescript --- elemIndex :: Eq a => a -> [a] -> Maybe Int on elemIndex(x, xs) -- Just the zero-based index of x in xs, -- or Nothing if x is not found in xs. set lng to length of xs repeat with i from 1 to lng if x = (item i of xs) then return Just(i - 1) end repeat return Nothing() end elemIndex ``` \ No newline at end of file diff --git a/MD applescript vs javascript/elemIndices.md b/MD applescript vs javascript/elemIndices.md index d0c40152..8d2be96b 100644 --- a/MD applescript vs javascript/elemIndices.md +++ b/MD applescript vs javascript/elemIndices.md @@ -1,15 +1,3 @@ -```javascript -// elemIndices :: Eq a => a -> [a] -> [Int] -const elemIndices = x => - // The indices at which x occurs in xs. - xs => [...xs].flatMap( - (y, i) => y === x ? ( - [i] - ) : [] - ); -``` - - ```applescript -- elemIndices :: Eq a => a -> [a] -> [Int] on elemIndices(x, xs) @@ -24,4 +12,16 @@ on elemIndices(x, xs) end script concatMap(result, xs) end elemIndices +``` + + +```javascript +// elemIndices :: Eq a => a -> [a] -> [Int] +const elemIndices = x => + // The indices at which x occurs in xs. + xs => [...xs].flatMap( + (y, i) => y === x ? ( + [i] + ) : [] + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/elems.md b/MD applescript vs javascript/elems.md index 1259b716..7553fd6d 100644 --- a/MD applescript vs javascript/elems.md +++ b/MD applescript vs javascript/elems.md @@ -1,13 +1,3 @@ -```javascript -// elems :: Map k a -> [a] -// elems :: Set a -> [a] -const elems = x => - 'Set' !== x.constructor.name ? ( - Object.values(x) - ) : Array.from(x.values()); -``` - - ```applescript -- elems :: Map k a -> [a] -- elems :: Set a -> [a] @@ -20,4 +10,14 @@ on elems(x) (allObjects() of x) as list end if end elems +``` + + +```javascript +// elems :: Map k a -> [a] +// elems :: Set a -> [a] +const elems = x => + 'Set' !== x.constructor.name ? ( + Object.values(x) + ) : Array.from(x.values()); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/encodedPath.md b/MD applescript vs javascript/encodedPath.md index 82f31e7c..e03b5f88 100644 --- a/MD applescript vs javascript/encodedPath.md +++ b/MD applescript vs javascript/encodedPath.md @@ -1,9 +1,3 @@ -```javascript -// encodedPath :: FilePath -> Percent Encoded String -const encodedPath = encodeURI; -``` - - ```applescript -- encodedPath :: FilePath -> Percent Encoded String on encodedPath(fp) @@ -13,4 +7,10 @@ on encodedPath(fp) stringWithString_(fp) of its NSString) as string end tell end encodedPath +``` + + +```javascript +// encodedPath :: FilePath -> Percent Encoded String +const encodedPath = encodeURI; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/enumFrom.md b/MD applescript vs javascript/enumFrom.md index a1eb0e4c..86427c3c 100644 --- a/MD applescript vs javascript/enumFrom.md +++ b/MD applescript vs javascript/enumFrom.md @@ -1,17 +1,3 @@ -```javascript -// enumFrom :: Enum a => a -> [a] -function* enumFrom(x) { - // A non-finite succession of enumerable - // values, starting with the value x. - let v = x; - while (true) { - yield v; - v = succ(v); - } -} -``` - - ```applescript -- enumFrom :: Enum a => a -> [a] on enumFrom(x) @@ -32,4 +18,18 @@ on enumFrom(x) end |λ| end script end enumFrom +``` + + +```javascript +// enumFrom :: Enum a => a -> [a] +function* enumFrom(x) { + // A non-finite succession of enumerable + // values, starting with the value x. + let v = x; + while (true) { + yield v; + v = succ(v); + } +} ``` \ No newline at end of file diff --git a/MD applescript vs javascript/enumFromPairs.md b/MD applescript vs javascript/enumFromPairs.md index b07c99b1..988159df 100644 --- a/MD applescript vs javascript/enumFromPairs.md +++ b/MD applescript vs javascript/enumFromPairs.md @@ -1,3 +1,21 @@ +```applescript +-- enumFromPairs :: String -> [(String, Int)] -> Dict +on enumFromPairs(strName, kvs) + set iMax to item 1 of item -1 of kvs + set iMin to item 1 of item 1 of kvs + script go + on |λ|(a, kv) + set {k, v} to kv + insertMap(insertMap(a, k, ¬ + {type:"enum", |name|:¬ + strName, |key|:k, min:iMin, max:iMax, value:v}), v, k) + end |λ| + end script + foldl(go, {|name|:strName, min:iMin, max:iMax}, kvs) +end enumFromPairs +``` + + ```javascript // enumFromPairs :: String -> [(String, Int)] -> Dict const enumFromPairs = name => @@ -23,22 +41,4 @@ const enumFromPairs = name => }, {} ); }; -``` - - -```applescript --- enumFromPairs :: String -> [(String, Int)] -> Dict -on enumFromPairs(strName, kvs) - set iMax to item 1 of item -1 of kvs - set iMin to item 1 of item 1 of kvs - script go - on |λ|(a, kv) - set {k, v} to kv - insertMap(insertMap(a, k, ¬ - {type:"enum", |name|:¬ - strName, |key|:k, min:iMin, max:iMax, value:v}), v, k) - end |λ| - end script - foldl(go, {|name|:strName, min:iMin, max:iMax}, kvs) -end enumFromPairs ``` \ No newline at end of file diff --git a/MD applescript vs javascript/enumFromThen.md b/MD applescript vs javascript/enumFromThen.md index 2b17b300..a7ca4a5a 100644 --- a/MD applescript vs javascript/enumFromThen.md +++ b/MD applescript vs javascript/enumFromThen.md @@ -1,22 +1,3 @@ -```javascript -// enumFromThen :: Int -> Int -> Gen [Int] -const enumFromThen = x => - // A non-finite stream of integers, - // starting with x and y, and continuing - // with the same interval. - function* (y) { - const d = y - x; - let v = y + d; - yield x; - yield y; - while (true) { - yield v; - v = d + v; - } - }; -``` - - ```applescript -- enumFromThen :: Int -> Int -> Gen [Int] on enumFromThen(m, n) @@ -33,4 +14,23 @@ on enumFromThen(m, n) end |λ| end script end enumFromThen +``` + + +```javascript +// enumFromThen :: Int -> Int -> Gen [Int] +const enumFromThen = x => + // A non-finite stream of integers, + // starting with x and y, and continuing + // with the same interval. + function* (y) { + const d = y - x; + let v = y + d; + yield x; + yield y; + while (true) { + yield v; + v = d + v; + } + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/enumFromThenTo.md b/MD applescript vs javascript/enumFromThenTo.md index b4702f81..8beabb86 100644 --- a/MD applescript vs javascript/enumFromThenTo.md +++ b/MD applescript vs javascript/enumFromThenTo.md @@ -1,15 +1,3 @@ -```javascript -// enumFromThenTo :: Int -> Int -> Int -> [Int] -const enumFromThenTo = x1 => - x2 => y => { - const d = x2 - x1; - return Array.from({ - length: Math.floor(y - x2) / d + 2 - }, (_, i) => x1 + (d * i)); - }; -``` - - ```applescript -- enumFromThenTo :: Int -> Int -> Int -> [Int] on enumFromThenTo(x1, x2, y) @@ -21,4 +9,16 @@ on enumFromThenTo(x1, x2, y) end repeat return xs end enumFromThenTo +``` + + +```javascript +// enumFromThenTo :: Int -> Int -> Int -> [Int] +const enumFromThenTo = x1 => + x2 => y => { + const d = x2 - x1; + return Array.from({ + length: Math.floor(y - x2) / d + 2 + }, (_, i) => x1 + (d * i)); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/enumFromThenToChar.md b/MD applescript vs javascript/enumFromThenToChar.md index c6ef34c8..e5644461 100644 --- a/MD applescript vs javascript/enumFromThenToChar.md +++ b/MD applescript vs javascript/enumFromThenToChar.md @@ -1,3 +1,16 @@ +```applescript +-- enumFromThenToChar :: Char -> Char -> Char -> [Char] +on enumFromThenToChar(x1, x2, y) + set {int1, int2, intY} to {id of x1, id of x2, id of y} + set xs to {} + repeat with i from int1 to intY by (int2 - int1) + set end of xs to character id i + end repeat + return xs +end enumFromThenToChar +``` + + ```javascript // enumFromThenToChar :: Char -> Char -> Char -> [Char] const enumFromThenToChar = x1 => @@ -9,17 +22,4 @@ const enumFromThenToChar = x1 => length: (Math.floor(iY - i2) / d) + 2 }, (_, i) => String.fromCodePoint(i1 + (d * i))); }; -``` - - -```applescript --- enumFromThenToChar :: Char -> Char -> Char -> [Char] -on enumFromThenToChar(x1, x2, y) - set {int1, int2, intY} to {id of x1, id of x2, id of y} - set xs to {} - repeat with i from int1 to intY by (int2 - int1) - set end of xs to character id i - end repeat - return xs -end enumFromThenToChar ``` \ No newline at end of file diff --git a/MD applescript vs javascript/enumFromTo.md b/MD applescript vs javascript/enumFromTo.md index e6901328..2103a6d4 100644 --- a/MD applescript vs javascript/enumFromTo.md +++ b/MD applescript vs javascript/enumFromTo.md @@ -1,14 +1,3 @@ -```javascript -// enumFromTo :: Int -> Int -> [Int] -const enumFromTo = m => - n => !isNaN(m) ? ( - Array.from({ - length: 1 + n - m - }, (_, i) => m + i) - ) : enumFromTo_(m)(n); -``` - - ```applescript -- enumFromTo :: Int -> Int -> [Int] on enumFromTo(m, n) @@ -22,4 +11,15 @@ on enumFromTo(m, n) {} end if end enumFromTo +``` + + +```javascript +// enumFromTo :: Int -> Int -> [Int] +const enumFromTo = m => + n => !isNaN(m) ? ( + Array.from({ + length: 1 + n - m + }, (_, i) => m + i) + ) : enumFromTo_(m)(n); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/enumFromToChar.md b/MD applescript vs javascript/enumFromToChar.md index aba6713a..44a4b480 100644 --- a/MD applescript vs javascript/enumFromToChar.md +++ b/MD applescript vs javascript/enumFromToChar.md @@ -1,14 +1,3 @@ -```javascript -// enumFromToChar :: Char -> Char -> [Char] -const enumFromToChar = m => n => { - const [intM, intN] = [m, n].map(x => x.codePointAt(0)); - return Array.from({ - length: Math.floor(intN - intM) + 1 - }, (_, i) => String.fromCodePoint(intM + i)); -}; -``` - - ```applescript -- enumFromToChar :: Char -> Char -> [Char] on enumFromToChar(m, n) @@ -23,4 +12,15 @@ on enumFromToChar(m, n) {} end if end enumFromToChar +``` + + +```javascript +// enumFromToChar :: Char -> Char -> [Char] +const enumFromToChar = m => n => { + const [intM, intN] = [m, n].map(x => x.codePointAt(0)); + return Array.from({ + length: Math.floor(intN - intM) + 1 + }, (_, i) => String.fromCodePoint(intM + i)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/enumFromTo_.md b/MD applescript vs javascript/enumFromTo_.md index 6af90633..48d83f19 100644 --- a/MD applescript vs javascript/enumFromTo_.md +++ b/MD applescript vs javascript/enumFromTo_.md @@ -1,16 +1,3 @@ -```javascript -// enumFromTo_ :: Enum a => a -> a -> [a] -const enumFromTo_ = m => n => { - const - [x, y] = [m, n].map(fromEnum), - b = x + (isNaN(m) ? 0 : m - x); - return Array.from({ - length: 1 + (y - x) - }, (_, i) => toEnum(m)(b + i)); -}; -``` - - ```applescript -- enumFromTo_ :: Enum a => a -> a -> [a] on enumFromTo_(m, n) @@ -26,4 +13,17 @@ on enumFromTo_(m, n) return {} end if end enumFromTo +``` + + +```javascript +// enumFromTo_ :: Enum a => a -> a -> [a] +const enumFromTo_ = m => n => { + const + [x, y] = [m, n].map(fromEnum), + b = x + (isNaN(m) ? 0 : m - x); + return Array.from({ + length: 1 + (y - x) + }, (_, i) => toEnum(m)(b + i)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/eq (==).md b/MD applescript vs javascript/eq (==).md index 8be8bc03..6e108d26 100644 --- a/MD applescript vs javascript/eq (==).md +++ b/MD applescript vs javascript/eq (==).md @@ -1,3 +1,11 @@ +```applescript +-- eq (==) :: Eq a => a -> a -> Bool +on eq(a, b) + a = b +end eq +``` + + ```javascript // eq (==) :: Eq a => a -> a -> Bool const eq = a => @@ -18,12 +26,4 @@ const eq = a => ) : kvs.every(([k, v]) => eq(v)(b[k])); })(); }; -``` - - -```applescript --- eq (==) :: Eq a => a -> a -> Bool -on eq(a, b) - a = b -end eq ``` \ No newline at end of file diff --git a/MD applescript vs javascript/evalJSLR.md b/MD applescript vs javascript/evalJSLR.md index 5a5e9230..edc68c76 100644 --- a/MD applescript vs javascript/evalJSLR.md +++ b/MD applescript vs javascript/evalJSLR.md @@ -1,15 +1,3 @@ -```javascript -// evalJSLR :: String -> Either String a -const evalJSLR = s => { - try { - return Right(eval('(' + s + ')')); - } catch (e) { - return Left(e.message); - } -}; -``` - - ```applescript -- gJSC can be declared in the global namespace, -- but unless the reference is released before the @@ -26,4 +14,16 @@ on evalJSLR(strJS) |Right|(v) end if end evalJSLR +``` + + +```javascript +// evalJSLR :: String -> Either String a +const evalJSLR = s => { + try { + return Right(eval('(' + s + ')')); + } catch (e) { + return Left(e.message); + } +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/evalJSMay.md b/MD applescript vs javascript/evalJSMay.md index b71f8442..cca8a7dd 100644 --- a/MD applescript vs javascript/evalJSMay.md +++ b/MD applescript vs javascript/evalJSMay.md @@ -1,15 +1,3 @@ -```javascript -// evalJSMay :: String -> Maybe a -const evalJSMay = s => { - try { - return Just(eval('(' + s + ')')); - } catch (e) { - return Nothing(); - } -}; -``` - - ```applescript -- use framework "Foundation" -- use framework "JavaScriptCore" @@ -35,4 +23,16 @@ on evalJSMay(strJS) Just(v) end if end evalJSMay +``` + + +```javascript +// evalJSMay :: String -> Maybe a +const evalJSMay = s => { + try { + return Just(eval('(' + s + ')')); + } catch (e) { + return Nothing(); + } +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/even.md b/MD applescript vs javascript/even.md index 6505e875..3619b0b0 100644 --- a/MD applescript vs javascript/even.md +++ b/MD applescript vs javascript/even.md @@ -1,12 +1,12 @@ -```javascript -// even :: Int -> Bool -const even = n => 0 === n % 2; -``` - - ```applescript -- even :: Int -> Bool on even(x) 0 = x mod 2 end even +``` + + +```javascript +// even :: Int -> Bool +const even = n => 0 === n % 2; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/exp.md b/MD applescript vs javascript/exp.md index c5a95d1a..31129146 100644 --- a/MD applescript vs javascript/exp.md +++ b/MD applescript vs javascript/exp.md @@ -1,12 +1,12 @@ -```javascript -// exp :: Float -> Float -const exp = Math.exp; -``` - - ```applescript -- exp :: Float -> Float on exp(n) Just of evalJSMay(("Math.exp(" & n as string) & ")") end exp +``` + + +```javascript +// exp :: Float -> Float +const exp = Math.exp; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fTable.md b/MD applescript vs javascript/fTable.md index d108a474..7daf8fef 100644 --- a/MD applescript vs javascript/fTable.md +++ b/MD applescript vs javascript/fTable.md @@ -1,3 +1,19 @@ +```applescript +-- fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String +on fTable(s, xShow, fxShow, f, xs) + set ys to map(xShow, xs) + set w to maximum(map(my |length|, ys)) + script arrowed + on |λ|(a, b) + justifyRight(w, space, a) & " -> " & b + end |λ| + end script + s & linefeed & unlines(zipWith(arrowed, ¬ + ys, map(compose(fxShow, f), xs))) +end fTable +``` + + ```javascript // fTable :: String -> (a -> String) -> // (b -> String) -> (a -> b) -> [a] -> String @@ -15,20 +31,4 @@ const fTable = s => xs.map(x => fxShow(f(x))) ).join('\n'); }; -``` - - -```applescript --- fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String -on fTable(s, xShow, fxShow, f, xs) - set ys to map(xShow, xs) - set w to maximum(map(my |length|, ys)) - script arrowed - on |λ|(a, b) - justifyRight(w, space, a) & " -> " & b - end |λ| - end script - s & linefeed & unlines(zipWith(arrowed, ¬ - ys, map(compose(fxShow, f), xs))) -end fTable ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fanArrow (&&&).md b/MD applescript vs javascript/fanArrow (&&&).md index 94457173..b2d3d099 100644 --- a/MD applescript vs javascript/fanArrow (&&&).md +++ b/MD applescript vs javascript/fanArrow (&&&).md @@ -1,14 +1,3 @@ -```javascript -// fanArrow (&&&) :: (a -> b) -> (a -> c) -> (a -> (b, c)) -const fanArrow = f => - // A function from x to a tuple of (f(x), g(x)) - // ((,) . f <*> g) - g => x => Tuple(f(x))( - g(x) - ); -``` - - ```applescript -- Compose a function from a simple value to a tuple of -- the separate outputs of two different functions @@ -20,4 +9,15 @@ on fanArrow(f, g) end |λ| end script end fanArrow +``` + + +```javascript +// fanArrow (&&&) :: (a -> b) -> (a -> c) -> (a -> (b, c)) +const fanArrow = f => + // A function from x to a tuple of (f(x), g(x)) + // ((,) . f <*> g) + g => x => Tuple(f(x))( + g(x) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/filePath.md b/MD applescript vs javascript/filePath.md index a66b9c33..add66a16 100644 --- a/MD applescript vs javascript/filePath.md +++ b/MD applescript vs javascript/filePath.md @@ -1,13 +1,3 @@ -```javascript -// filePath :: String -> FilePath -const filePath = s => - // The given file path with any tilde expanded - // to the full user directory path. - ObjC.unwrap(ObjC.wrap(s) - .stringByStandardizingPath); -``` - - ```applescript -- filePath :: String -> FilePath on filePath(s) @@ -15,4 +5,14 @@ on filePath(s) NSString's stringWithString:s)'s ¬ stringByStandardizingPath()) as string end filePath +``` + + +```javascript +// filePath :: String -> FilePath +const filePath = s => + // The given file path with any tilde expanded + // to the full user directory path. + ObjC.unwrap(ObjC.wrap(s) + .stringByStandardizingPath); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/filePathTree.md b/MD applescript vs javascript/filePathTree.md index 856f1117..cbbeac46 100644 --- a/MD applescript vs javascript/filePathTree.md +++ b/MD applescript vs javascript/filePathTree.md @@ -1,19 +1,3 @@ -```javascript -// filePathTree :: filePath -> [Tree String] -> Tree FilePath -const filePathTree = fpAnchor => trees => { - const go = fp => tree => { - const path = `${fp}/${tree.root}`; - return Node(path)( - tree.nest.map(go(path)) - ); - }; - return Node(fpAnchor)( - trees.map(go(fpAnchor)) - ); -}; -``` - - ```applescript -- filePathTree :: filePath -> [Tree String] -> Tree FilePath on filePathTree(fpAnchor, trees) @@ -31,4 +15,20 @@ on filePathTree(fpAnchor, trees) Node(fpAnchor, map(go's |λ|(fpAnchor), trees)) end filePathTree +``` + + +```javascript +// filePathTree :: filePath -> [Tree String] -> Tree FilePath +const filePathTree = fpAnchor => trees => { + const go = fp => tree => { + const path = `${fp}/${tree.root}`; + return Node(path)( + tree.nest.map(go(path)) + ); + }; + return Node(fpAnchor)( + trees.map(go(fpAnchor)) + ); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fileSize.md b/MD applescript vs javascript/fileSize.md index fc240f57..06325b39 100644 --- a/MD applescript vs javascript/fileSize.md +++ b/MD applescript vs javascript/fileSize.md @@ -1,12 +1,3 @@ -```javascript -// fileSize :: FilePath -> Either String Int -const fileSize = fp => - bindLR(fileStatus(fp))( - dct => Right(ObjC.unwrap(dct.NSFileSize)) - ); -``` - - ```applescript -- fileSize :: FilePath -> Either String Int on fileSize(fp) @@ -17,4 +8,13 @@ on fileSize(fp) end script bindLR(my fileStatus(fp), fs) end fileSize +``` + + +```javascript +// fileSize :: FilePath -> Either String Int +const fileSize = fp => + bindLR(fileStatus(fp))( + dct => Right(ObjC.unwrap(dct.NSFileSize)) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fileStatus.md b/MD applescript vs javascript/fileStatus.md index d278cbe2..9eb70a93 100644 --- a/MD applescript vs javascript/fileStatus.md +++ b/MD applescript vs javascript/fileStatus.md @@ -1,17 +1,3 @@ -```javascript -// fileStatus :: FilePath -> Either String Dict -const fileStatus = fp => { - const - e = $(), - dct = $.NSFileManager.defaultManager - .attributesOfItemAtPathError(fp, e); - return dct.isNil() ? ( - Left(ObjC.unwrap(e.localizedDescription)) - ) : Right(ObjC.deepUnwrap(dct)); -}; -``` - - ```applescript -- fileStatus :: FilePath -> Either String Dict on fileStatus(fp) @@ -24,4 +10,18 @@ on fileStatus(fp) |Left|((localizedDescription of e) as string) end if end fileStatus +``` + + +```javascript +// fileStatus :: FilePath -> Either String Dict +const fileStatus = fp => { + const + e = $(), + dct = $.NSFileManager.defaultManager + .attributesOfItemAtPathError(fp, e); + return dct.isNil() ? ( + Left(ObjC.unwrap(e.localizedDescription)) + ) : Right(ObjC.deepUnwrap(dct)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fileUTI.md b/MD applescript vs javascript/fileUTI.md index e892d2de..afaf8779 100644 --- a/MD applescript vs javascript/fileUTI.md +++ b/MD applescript vs javascript/fileUTI.md @@ -1,18 +1,3 @@ -```javascript -// fileUTI :: FilePath -> Either String String -const fileUTI = fp => { - // ObjC.import('AppKit') - const - e = $(), - uti = $.NSWorkspace.sharedWorkspace - .typeOfFileError(fp, e); - return uti.isNil() ? ( - Left(ObjC.unwrap(e.localizedDescription)) - ) : Right(ObjC.unwrap(uti)); -}; -``` - - ```applescript -- fileUTI :: FilePath -> Either String String on fileUTI(fp) @@ -25,4 +10,19 @@ on fileUTI(fp) |Right|(uti as text) end if end fileUTI +``` + + +```javascript +// fileUTI :: FilePath -> Either String String +const fileUTI = fp => { + // ObjC.import('AppKit') + const + e = $(), + uti = $.NSWorkspace.sharedWorkspace + .typeOfFileError(fp, e); + return uti.isNil() ? ( + Left(ObjC.unwrap(e.localizedDescription)) + ) : Right(ObjC.unwrap(uti)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/filter.md b/MD applescript vs javascript/filter.md index cf5051c5..28bdcd33 100644 --- a/MD applescript vs javascript/filter.md +++ b/MD applescript vs javascript/filter.md @@ -1,12 +1,3 @@ -```javascript -// filter :: (a -> Bool) -> [a] -> [a] -const filter = p => - // The elements of xs which match - // the predicate p. - xs => [...xs].filter(p); -``` - - ```applescript -- filter :: (a -> Bool) -> [a] -> [a] on filter(p, xs) @@ -24,4 +15,13 @@ on filter(p, xs) end if end tell end filter +``` + + +```javascript +// filter :: (a -> Bool) -> [a] -> [a] +const filter = p => + // The elements of xs which match + // the predicate p. + xs => [...xs].filter(p); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/filterGen.md b/MD applescript vs javascript/filterGen.md index bd850187..ba27ff32 100644 --- a/MD applescript vs javascript/filterGen.md +++ b/MD applescript vs javascript/filterGen.md @@ -1,3 +1,22 @@ +```applescript +-- filterGen :: (a -> Bool) -> Gen [a] -> Gen [a] +on filterGen(p, gen) + -- Non-finite stream of values which are + -- drawn from gen, and satisfy p + script + property mp : mReturn(p)'s |λ| + on |λ|() + set v to gen's |λ|() + repeat until mp(v) + set v to gen's |λ|() + end repeat + return v + end |λ| + end script +end filterGen +``` + + ```javascript // filterGen :: (a -> Bool) -> Gen [a] -> [a] const filterGen = p => xs => { @@ -15,23 +34,4 @@ const filterGen = p => xs => { } return go(xs); }; -``` - - -```applescript --- filterGen :: (a -> Bool) -> Gen [a] -> Gen [a] -on filterGen(p, gen) - -- Non-finite stream of values which are - -- drawn from gen, and satisfy p - script - property mp : mReturn(p)'s |λ| - on |λ|() - set v to gen's |λ|() - repeat until mp(v) - set v to gen's |λ|() - end repeat - return v - end |λ| - end script -end filterGen ``` \ No newline at end of file diff --git a/MD applescript vs javascript/find.md b/MD applescript vs javascript/find.md index 008eb02f..e35eb819 100644 --- a/MD applescript vs javascript/find.md +++ b/MD applescript vs javascript/find.md @@ -1,3 +1,17 @@ +```applescript +-- find :: (a -> Bool) -> [a] -> Maybe a +on find(p, xs) + tell mReturn(p) + set lng to length of xs + repeat with i from 1 to lng + if |λ|(item i of xs) then return Just(item i of xs) + end repeat + Nothing() + end tell +end find +``` + + ```javascript // find :: (a -> Bool) -> [a] -> Maybe a const find = p => @@ -14,18 +28,4 @@ const find = p => Just(ys[i]) ) : Nothing(); })() : findGen(p)(xs); -``` - - -```applescript --- find :: (a -> Bool) -> [a] -> Maybe a -on find(p, xs) - tell mReturn(p) - set lng to length of xs - repeat with i from 1 to lng - if |λ|(item i of xs) then return Just(item i of xs) - end repeat - Nothing() - end tell -end find ``` \ No newline at end of file diff --git a/MD applescript vs javascript/findIndex.md b/MD applescript vs javascript/findIndex.md index 11fe4be7..d0fa2853 100644 --- a/MD applescript vs javascript/findIndex.md +++ b/MD applescript vs javascript/findIndex.md @@ -1,18 +1,3 @@ -```javascript -// findIndex :: (a -> Bool) -> [a] -> Maybe Int -const findIndex = p => - // Just the index of the first element in - // xs for which p(x) is true, or - // Nothing if there is no such element. - xs => { - const i = [...xs].findIndex(p); - return -1 !== i ? ( - Just(i) - ) : Nothing(); - }; -``` - - ```applescript -- findIndex :: (a -> Bool) -> [a] -> Maybe Int on findIndex(p, xs) @@ -27,4 +12,19 @@ on findIndex(p, xs) return Nothing() end tell end findIndex +``` + + +```javascript +// findIndex :: (a -> Bool) -> [a] -> Maybe Int +const findIndex = p => + // Just the index of the first element in + // xs for which p(x) is true, or + // Nothing if there is no such element. + xs => { + const i = [...xs].findIndex(p); + return -1 !== i ? ( + Just(i) + ) : Nothing(); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/findIndexR.md b/MD applescript vs javascript/findIndexR.md index 702a716a..ea1f0a77 100644 --- a/MD applescript vs javascript/findIndexR.md +++ b/MD applescript vs javascript/findIndexR.md @@ -1,18 +1,3 @@ -```javascript -// findIndexR :: (a -> Bool) -> [a] -> Maybe Int -const findIndexR = p => - // Just the index of the last element in - // xs for which p(x) is true, or - // Nothing if there is no such element. - xs => { - const i = reverse([...xs]).findIndex(p); - return -1 !== i ? ( - Just(xs.length - (1 + i)) - ) : Nothing(); - }; -``` - - ```applescript -- findIndexR :: (a -> Bool) -> [a] -> Maybe Int on findIndexR(p, xs) @@ -27,4 +12,19 @@ on findIndexR(p, xs) return Nothing() end tell end findIndexR +``` + + +```javascript +// findIndexR :: (a -> Bool) -> [a] -> Maybe Int +const findIndexR = p => + // Just the index of the last element in + // xs for which p(x) is true, or + // Nothing if there is no such element. + xs => { + const i = reverse([...xs]).findIndex(p); + return -1 !== i ? ( + Just(xs.length - (1 + i)) + ) : Nothing(); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/findIndices.md b/MD applescript vs javascript/findIndices.md index 80811d92..7ebc6081 100644 --- a/MD applescript vs javascript/findIndices.md +++ b/MD applescript vs javascript/findIndices.md @@ -1,15 +1,3 @@ -```javascript -// findIndices :: (a -> Bool) -> [a] -> [Int] -// findIndices :: (String -> Bool) -> String -> [Int] -const findIndices = p => - xs => ( - ys => ys.flatMap((y, i) => p(y, i, ys) ? ( - [i] - ) : []) - )([...xs]); -``` - - ```applescript -- findIndices :: (a -> Bool) -> [a] -> [Int] on findIndices(p, xs) @@ -27,4 +15,16 @@ on findIndices(p, xs) end script concatMap(result, xs) end findIndices +``` + + +```javascript +// findIndices :: (a -> Bool) -> [a] -> [Int] +// findIndices :: (String -> Bool) -> String -> [Int] +const findIndices = p => + xs => ( + ys => ys.flatMap((y, i) => p(y, i, ys) ? ( + [i] + ) : []) + )([...xs]); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/findTree.md b/MD applescript vs javascript/findTree.md index 0a30191d..ca6eefe3 100644 --- a/MD applescript vs javascript/findTree.md +++ b/MD applescript vs javascript/findTree.md @@ -1,32 +1,3 @@ -```javascript -// findTree :: (a -> Bool) -> Tree a -> Maybe Tree a -const findTree = p => { - // The first of any nodes in the tree which match the predicate p - // (For all matches, see treeMatches) - const go = tree => - p(tree.root) ? ( - Just(tree) - ) : (() => { - const - xs = tree.nest, - lng = xs.length; - return 0 < lng ? until( - tpl => lng <= tpl[0] || !tpl[1].Nothing - )( - tpl => Tuple(1 + tpl[0])( - go(xs[tpl[0]]) - ) - )( - Tuple(0)( - Nothing() - ) - )[1] : Nothing(); - })(); - return go; -}; -``` - - ```applescript -- The first of any nodes in the tree which match the predicate p -- (For all matches, see treeMatches) @@ -64,4 +35,33 @@ on findTree(p, tree) go's |λ|(tree) end findTree +``` + + +```javascript +// findTree :: (a -> Bool) -> Tree a -> Maybe Tree a +const findTree = p => { + // The first of any nodes in the tree which match the predicate p + // (For all matches, see treeMatches) + const go = tree => + p(tree.root) ? ( + Just(tree) + ) : (() => { + const + xs = tree.nest, + lng = xs.length; + return 0 < lng ? until( + tpl => lng <= tpl[0] || !tpl[1].Nothing + )( + tpl => Tuple(1 + tpl[0])( + go(xs[tpl[0]]) + ) + )( + Tuple(0)( + Nothing() + ) + )[1] : Nothing(); + })(); + return go; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/first.md b/MD applescript vs javascript/first.md index c3047f9a..2d87bbf5 100644 --- a/MD applescript vs javascript/first.md +++ b/MD applescript vs javascript/first.md @@ -1,14 +1,3 @@ -```javascript -// first :: (a -> b) -> ((a, c) -> (b, c)) -const first = f => - // A simple function lifted to one which applies - // to a tuple, transforming only its first item. - xy => Tuple(f(xy[0]))( - xy[1] - ); -``` - - ```applescript -- first :: (a -> b) -> ((a, c) -> (b, c)) on |first|(f) @@ -20,4 +9,15 @@ on |first|(f) end |λ| end script end |first| +``` + + +```javascript +// first :: (a -> b) -> ((a, c) -> (b, c)) +const first = f => + // A simple function lifted to one which applies + // to a tuple, transforming only its first item. + xy => Tuple(f(xy[0]))( + xy[1] + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/flatten.md b/MD applescript vs javascript/flatten.md index c3b610f0..5060b51a 100644 --- a/MD applescript vs javascript/flatten.md +++ b/MD applescript vs javascript/flatten.md @@ -1,10 +1,3 @@ -```javascript -// flatten :: NestedList a -> [a] -const flatten = nest => - nest.flat(Infinity); -``` - - ```applescript -- flatten :: NestedList a -> [a] on flatten(t) @@ -15,4 +8,11 @@ on flatten(t) t end if end flatten +``` + + +```javascript +// flatten :: NestedList a -> [a] +const flatten = nest => + nest.flat(Infinity); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/flattenTree.md b/MD applescript vs javascript/flattenTree.md index f917909d..e77d3e0d 100644 --- a/MD applescript vs javascript/flattenTree.md +++ b/MD applescript vs javascript/flattenTree.md @@ -1,15 +1,3 @@ -```javascript -// flattenTree :: Tree a -> [a] -const flattenTree = tree => { - const - go = (xs, node) => [node.root].concat( - node.nest.reduceRight(go, xs) - ); - return go([], tree); -}; -``` - - ```applescript -- The root elements of a tree in pre-order. -- flattenTree :: Tree a -> [a] @@ -21,4 +9,16 @@ on flattenTree(node) end script go's |λ|(node, {}) end flattenTree +``` + + +```javascript +// flattenTree :: Tree a -> [a] +const flattenTree = tree => { + const + go = (xs, node) => [node.root].concat( + node.nest.reduceRight(go, xs) + ); + return go([], tree); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/flip.md b/MD applescript vs javascript/flip.md index e03c5e3c..6cb71d3d 100644 --- a/MD applescript vs javascript/flip.md +++ b/MD applescript vs javascript/flip.md @@ -1,14 +1,3 @@ -```javascript -// flip :: (a -> b -> c) -> b -> a -> c -const flip = op => - // The binary function op with - // its arguments reversed. - 1 < op.length ? ( - (a, b) => op(b, a) - ) : (x => y => op(y)(x)); -``` - - ```applescript -- flip :: (a -> b -> c) -> b -> a -> c on flip(f) @@ -19,4 +8,15 @@ on flip(f) end |λ| end script end flip +``` + + +```javascript +// flip :: (a -> b -> c) -> b -> a -> c +const flip = op => + // The binary function op with + // its arguments reversed. + 1 < op.length ? ( + (a, b) => op(b, a) + ) : (x => y => op(y)(x)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/floor.md b/MD applescript vs javascript/floor.md index 709dceba..b8b7b536 100644 --- a/MD applescript vs javascript/floor.md +++ b/MD applescript vs javascript/floor.md @@ -1,18 +1,3 @@ -```javascript -// floor :: Num -> Int -const floor = x => { - const - nr = ( - 'Ratio' !== x.type ? ( - properFraction - ) : properFracRatio - )(x), - n = nr[0]; - return 0 > nr[1] ? n - 1 : n; -}; -``` - - ```applescript -- floor :: Num -> Int on floor(x) @@ -28,4 +13,19 @@ on floor(x) n end if end floor +``` + + +```javascript +// floor :: Num -> Int +const floor = x => { + const + nr = ( + 'Ratio' !== x.type ? ( + properFraction + ) : properFracRatio + )(x), + n = nr[0]; + return 0 > nr[1] ? n - 1 : n; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fmap (<$>).md b/MD applescript vs javascript/fmap (<$>).md index 4533f9c4..e426e990 100644 --- a/MD applescript vs javascript/fmap (<$>).md +++ b/MD applescript vs javascript/fmap (<$>).md @@ -1,23 +1,3 @@ -```javascript -// fmap (<$>) :: Functor f => (a -> b) -> f a -> f b -const fmap = f => fa => - Array.isArray(fa) ? ( - fa.map(f) - ) : 'string' !== typeof fa ? (() => { - const t = fa.type; - return ('Either' === t ? ( - fmapLR(f)(fa) - ) : 'Maybe' === t ? ( - fmapMay(f)(fa) - ) : 'Node' === t ? ( - fmapTree(f)(fa) - ) : 'Tuple' === t ? ( - fmapTuple(f)(fa) - ) : undefined); - })() : fa.split('').map(f); -``` - - ```applescript -- fmap (<$>) :: Functor f => (a -> b) -> f a -> f b on fmap(f, fa) @@ -44,4 +24,24 @@ on fmap(f, fa) missing value end if end fmap +``` + + +```javascript +// fmap (<$>) :: Functor f => (a -> b) -> f a -> f b +const fmap = f => fa => + Array.isArray(fa) ? ( + fa.map(f) + ) : 'string' !== typeof fa ? (() => { + const t = fa.type; + return ('Either' === t ? ( + fmapLR(f)(fa) + ) : 'Maybe' === t ? ( + fmapMay(f)(fa) + ) : 'Node' === t ? ( + fmapTree(f)(fa) + ) : 'Tuple' === t ? ( + fmapTuple(f)(fa) + ) : undefined); + })() : fa.split('').map(f); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fmapLR (<$>).md b/MD applescript vs javascript/fmapLR (<$>).md index 83935f10..16f7d1af 100644 --- a/MD applescript vs javascript/fmapLR (<$>).md +++ b/MD applescript vs javascript/fmapLR (<$>).md @@ -1,12 +1,3 @@ -```javascript -// fmapLR (<$>) :: (a -> b) -> Either a a -> Either a b -const fmapLR = f => lr => - undefined === lr.Left ? ( - Right(f(lr.Right)) - ) : lr; -``` - - ```applescript -- fmapLR (<$>) :: (a -> b) -> Either a a -> Either a b on fmapLR(f, lr) @@ -16,4 +7,13 @@ on fmapLR(f, lr) lr end if end fmapLR +``` + + +```javascript +// fmapLR (<$>) :: (a -> b) -> Either a a -> Either a b +const fmapLR = f => lr => + undefined === lr.Left ? ( + Right(f(lr.Right)) + ) : lr; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fmapMay (<$>).md b/MD applescript vs javascript/fmapMay (<$>).md index 350a0bb9..d787f9a7 100644 --- a/MD applescript vs javascript/fmapMay (<$>).md +++ b/MD applescript vs javascript/fmapMay (<$>).md @@ -1,12 +1,3 @@ -```javascript -// fmapMay (<$>) :: (a -> b) -> Maybe a -> Maybe b -const fmapMay = f => mb => - mb.Nothing ? ( - mb - ) : Just(f(mb.Just)); -``` - - ```applescript -- fmapMay (<$>) :: (a -> b) -> Maybe a -> Maybe b on fmapMay(f, mb) @@ -16,4 +7,13 @@ on fmapMay(f, mb) Just(|λ|(Just of mb) of mReturn(f)) end if end fmapMay +``` + + +```javascript +// fmapMay (<$>) :: (a -> b) -> Maybe a -> Maybe b +const fmapMay = f => mb => + mb.Nothing ? ( + mb + ) : Just(f(mb.Just)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fmapTree (<$>).md b/MD applescript vs javascript/fmapTree (<$>).md index ffa2ce33..728f2c6c 100644 --- a/MD applescript vs javascript/fmapTree (<$>).md +++ b/MD applescript vs javascript/fmapTree (<$>).md @@ -1,16 +1,3 @@ -```javascript -// fmapTree :: (a -> b) -> Tree a -> Tree b -const fmapTree = f => { - // A new tree. The result of a structure-preserving - // application of f to each root in the existing tree. - const go = tree => Node(f(tree.root))( - tree.nest.map(go) - ); - return go; -}; -``` - - ```applescript -- fmapTree :: (a -> b) -> Tree a -> Tree b on fmapTree(f, tree) @@ -28,4 +15,17 @@ on fmapTree(f, tree) end script |λ|(tree) of go end fmapTree +``` + + +```javascript +// fmapTree :: (a -> b) -> Tree a -> Tree b +const fmapTree = f => { + // A new tree. The result of a structure-preserving + // application of f to each root in the existing tree. + const go = tree => Node(f(tree.root))( + tree.nest.map(go) + ); + return go; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fmapTuple (<$>).md b/MD applescript vs javascript/fmapTuple (<$>).md index 4b5f0b1b..e513d774 100644 --- a/MD applescript vs javascript/fmapTuple (<$>).md +++ b/MD applescript vs javascript/fmapTuple (<$>).md @@ -1,15 +1,15 @@ +```applescript +-- fmapTuple (<$>) :: (a -> b) -> (a, a) -> (a, b) +on fmapTuple(f, tpl) + Tuple(|1| of tpl, |λ|(|2| of tpl) of mReturn(f)) +end fmapTuple +``` + + ```javascript // fmapTuple (<$>) :: (a -> b) -> (a, a) -> (a, b) const fmapTuple = f => tpl => Tuple(tpl[0])( f(tpl[1]) ); -``` - - -```applescript --- fmapTuple (<$>) :: (a -> b) -> (a, a) -> (a, b) -on fmapTuple(f, tpl) - Tuple(|1| of tpl, |λ|(|2| of tpl) of mReturn(f)) -end fmapTuple ``` \ No newline at end of file diff --git a/MD applescript vs javascript/foldMapTree.md b/MD applescript vs javascript/foldMapTree.md index 75f4ea5c..310b56d5 100644 --- a/MD applescript vs javascript/foldMapTree.md +++ b/MD applescript vs javascript/foldMapTree.md @@ -1,17 +1,3 @@ -```javascript -// foldMapTree :: Monoid m => (a -> m) -> Tree a -> m -const foldMapTree = f => { - // Result of mapping each element of the tree to - // a monoid, and combining with mappend. - const go = tree => - 0 < tree.nest.length ? mappend(f(tree.root))( - foldl1(mappend)(tree.nest.map(go)) - ) : f(tree.root); - return go; -}; -``` - - ```applescript -- foldMapTree :: Monoid m => (a -> m) -> Tree a -> m on foldMapTree(f, tree) @@ -28,4 +14,18 @@ on foldMapTree(f, tree) end script |λ|(tree) of go end foldMapTree +``` + + +```javascript +// foldMapTree :: Monoid m => (a -> m) -> Tree a -> m +const foldMapTree = f => { + // Result of mapping each element of the tree to + // a monoid, and combining with mappend. + const go = tree => + 0 < tree.nest.length ? mappend(f(tree.root))( + foldl1(mappend)(tree.nest.map(go)) + ) : f(tree.root); + return go; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/foldTree.md b/MD applescript vs javascript/foldTree.md index 24ff2fc5..a1814022 100644 --- a/MD applescript vs javascript/foldTree.md +++ b/MD applescript vs javascript/foldTree.md @@ -1,16 +1,3 @@ -```javascript -// foldTree :: (a -> [b] -> b) -> Tree a -> b -const foldTree = f => { - // The catamorphism on trees. A summary - // value obtained by a depth-first fold. - const go = tree => f(tree.root)( - tree.nest.map(go) - ); - return go; -}; -``` - - ```applescript -- foldTree :: (a -> [b] -> b) -> Tree a -> b on foldTree(f, tree) @@ -22,4 +9,17 @@ on foldTree(f, tree) end script |λ|(tree) of go end foldTree +``` + + +```javascript +// foldTree :: (a -> [b] -> b) -> Tree a -> b +const foldTree = f => { + // The catamorphism on trees. A summary + // value obtained by a depth-first fold. + const go = tree => f(tree.root)( + tree.nest.map(go) + ); + return go; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/foldl.md b/MD applescript vs javascript/foldl.md index 176ce978..05706eaf 100644 --- a/MD applescript vs javascript/foldl.md +++ b/MD applescript vs javascript/foldl.md @@ -1,13 +1,3 @@ -```javascript -// foldl :: (a -> b -> a) -> a -> [b] -> a -const foldl = f => - a => xs => [...xs].reduce( - (x, y) => f(x)(y), - a - ); -``` - - ```applescript -- foldl :: (a -> b -> a) -> a -> [b] -> a on foldl(f, startValue, xs) @@ -20,4 +10,14 @@ on foldl(f, startValue, xs) return v end tell end foldl +``` + + +```javascript +// foldl :: (a -> b -> a) -> a -> [b] -> a +const foldl = f => + a => xs => [...xs].reduce( + (x, y) => f(x)(y), + a + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/foldl1.md b/MD applescript vs javascript/foldl1.md index 450b2c4b..873c1c54 100644 --- a/MD applescript vs javascript/foldl1.md +++ b/MD applescript vs javascript/foldl1.md @@ -1,16 +1,3 @@ -```javascript -// foldl1 :: (a -> a -> a) -> [a] -> a -const foldl1 = f => - // Left to right reduction of the non-empty list xs, - // using the binary operator f, with the head of xs - // as the initial acccumulator value. - xs => ( - ys => 1 < ys.length ? ys.slice(1) - .reduce(uncurry(f), ys[0]) : ys[0] - )(list(xs)); -``` - - ```applescript -- foldl1 :: (a -> a -> a) -> [a] -> a on foldl1(f, xs) @@ -27,4 +14,17 @@ on foldl1(f, xs) item 1 of xs end if end foldl1 +``` + + +```javascript +// foldl1 :: (a -> a -> a) -> [a] -> a +const foldl1 = f => + // Left to right reduction of the non-empty list xs, + // using the binary operator f, with the head of xs + // as the initial acccumulator value. + xs => ( + ys => 1 < ys.length ? ys.slice(1) + .reduce(uncurry(f), ys[0]) : ys[0] + )(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/foldl1May.md b/MD applescript vs javascript/foldl1May.md index 79f8b143..00315144 100644 --- a/MD applescript vs javascript/foldl1May.md +++ b/MD applescript vs javascript/foldl1May.md @@ -1,15 +1,3 @@ -```javascript -// foldl1May :: (a -> a -> a) -> [a] -> Maybe a -const foldl1May = f => - xs => ( - ys => 0 < ys.length ? ( - Just(ys.slice(1) - .reduce(uncurry(f), ys[0])) - ) : Nothing() - )(list(xs)); -``` - - ```applescript -- foldl1May :: (a -> a -> a) -> [a] -> Maybe a on foldl1May(f, xs) @@ -31,4 +19,16 @@ on foldl1May(f, xs) Nothing() end if end foldl1May +``` + + +```javascript +// foldl1May :: (a -> a -> a) -> [a] -> Maybe a +const foldl1May = f => + xs => ( + ys => 0 < ys.length ? ( + Just(ys.slice(1) + .reduce(uncurry(f), ys[0])) + ) : Nothing() + )(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/foldlTree.md b/MD applescript vs javascript/foldlTree.md index f47feb2e..adb99c46 100644 --- a/MD applescript vs javascript/foldlTree.md +++ b/MD applescript vs javascript/foldlTree.md @@ -1,14 +1,3 @@ -```javascript -// foldlTree :: (b -> a -> b) -> b -> Tree a -> b -const foldlTree = f => - acc => node => { - const go = (a, x) => - x.nest.reduce(go, f(a)(x.root)); - return go(acc, node); - }; -``` - - ```applescript -- foldlTree :: (b -> a -> b) -> b -> Tree a -> b on foldlTree(f, acc, tree) @@ -25,4 +14,15 @@ on foldlTree(f, acc, tree) end script |λ|(acc, tree) of go end foldlTree +``` + + +```javascript +// foldlTree :: (b -> a -> b) -> b -> Tree a -> b +const foldlTree = f => + acc => node => { + const go = (a, x) => + x.nest.reduce(go, f(a)(x.root)); + return go(acc, node); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/foldr.md b/MD applescript vs javascript/foldr.md index 81414615..b6193c3c 100644 --- a/MD applescript vs javascript/foldr.md +++ b/MD applescript vs javascript/foldr.md @@ -1,15 +1,3 @@ -```javascript -// foldr :: (a -> b -> b) -> b -> [a] -> b -const foldr = f => - // Note that that the Haskell signature of foldr differs from that of - // foldl - the positions of accumulator and current value are reversed - a => xs => [...xs].reduceRight( - (a, x) => f(x)(a), - a - ); -``` - - ```applescript -- foldr :: (a -> b -> b) -> b -> [a] -> b on foldr(f, startValue, xs) @@ -22,4 +10,16 @@ on foldr(f, startValue, xs) return v end tell end foldr +``` + + +```javascript +// foldr :: (a -> b -> b) -> b -> [a] -> b +const foldr = f => + // Note that that the Haskell signature of foldr differs from that of + // foldl - the positions of accumulator and current value are reversed + a => xs => [...xs].reduceRight( + (a, x) => f(x)(a), + a + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/foldr1.md b/MD applescript vs javascript/foldr1.md index 55f27887..2e5e7f46 100644 --- a/MD applescript vs javascript/foldr1.md +++ b/MD applescript vs javascript/foldr1.md @@ -1,15 +1,3 @@ -```javascript -// foldr1 :: (a -> a -> a) -> [a] -> a -const foldr1 = f => - xs => (ys => 0 < ys.length ? ( - init(ys).reduceRight( - uncurry(f), - last(ys) - ) - ) : [])(list(xs)); -``` - - ```applescript -- foldr1 :: (a -> a -> a) -> [a] -> a on foldr1(f, xs) @@ -26,4 +14,16 @@ on foldr1(f, xs) xs end if end foldr1 +``` + + +```javascript +// foldr1 :: (a -> a -> a) -> [a] -> a +const foldr1 = f => + xs => (ys => 0 < ys.length ? ( + init(ys).reduceRight( + uncurry(f), + last(ys) + ) + ) : [])(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/foldr1May.md b/MD applescript vs javascript/foldr1May.md index 53f6ee64..05f3d0eb 100644 --- a/MD applescript vs javascript/foldr1May.md +++ b/MD applescript vs javascript/foldr1May.md @@ -1,18 +1,3 @@ -```javascript -// foldr1May :: (a -> a -> a) -> [a] -> Maybe a -const foldr1May = f => - // Nothing if xs is empty, or Just a right - // fold of f over the list using the last - // item of xs as the initial accumulator value. - xs => ( - ys => 0 < ys.length ? ( - Just(ys.slice(0, -1) - .reduceRight(uncurry(f), ys.slice(-1)[0])) - ) : Nothing() - )(list(xs)); -``` - - ```applescript -- foldr1May :: (a -> a -> a) -> [a] -> Maybe a on foldr1May(f, xs) @@ -29,4 +14,19 @@ on foldr1May(f, xs) Nothing() end if end foldr1May +``` + + +```javascript +// foldr1May :: (a -> a -> a) -> [a] -> Maybe a +const foldr1May = f => + // Nothing if xs is empty, or Just a right + // fold of f over the list using the last + // item of xs as the initial accumulator value. + xs => ( + ys => 0 < ys.length ? ( + Just(ys.slice(0, -1) + .reduceRight(uncurry(f), ys.slice(-1)[0])) + ) : Nothing() + )(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/foldrTree.md b/MD applescript vs javascript/foldrTree.md index 474ab521..2e285491 100644 --- a/MD applescript vs javascript/foldrTree.md +++ b/MD applescript vs javascript/foldrTree.md @@ -1,16 +1,3 @@ -```javascript -// foldrTree :: (a -> b -> b) -> b -> Tree a -> b -const foldrTree = f => - acc => node => { - const go = (a, x) => - f(x.root)( - x.nest.reduceRight(go, a) - ); - return go(acc, node); - }; -``` - - ```applescript -- foldrTree :: (a -> b -> b) -> b -> Tree a -> b on foldrTree(f, acc, tree) @@ -22,4 +9,17 @@ on foldrTree(f, acc, tree) end script |λ|(tree, acc) of go end foldrTree +``` + + +```javascript +// foldrTree :: (a -> b -> b) -> b -> Tree a -> b +const foldrTree = f => + acc => node => { + const go = (a, x) => + f(x.root)( + x.nest.reduceRight(go, a) + ); + return go(acc, node); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fromEnum.md b/MD applescript vs javascript/fromEnum.md index 9e843bcf..96791ff8 100644 --- a/MD applescript vs javascript/fromEnum.md +++ b/MD applescript vs javascript/fromEnum.md @@ -1,14 +1,3 @@ -```javascript -// fromEnum :: Enum a => a -> Int -const fromEnum = x => - typeof x !== 'string' ? ( - x.constructor === Object ? ( - x.value - ) : parseInt(Number(x)) - ) : x.codePointAt(0); -``` - - ```applescript -- fromEnum :: Enum a => a -> Int on fromEnum(x) @@ -29,4 +18,15 @@ on fromEnum(x) x as integer end if end fromEnum +``` + + +```javascript +// fromEnum :: Enum a => a -> Int +const fromEnum = x => + typeof x !== 'string' ? ( + x.constructor === Object ? ( + x.value + ) : parseInt(Number(x)) + ) : x.codePointAt(0); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fromLeft.md b/MD applescript vs javascript/fromLeft.md index c1e47712..fe970df7 100644 --- a/MD applescript vs javascript/fromLeft.md +++ b/MD applescript vs javascript/fromLeft.md @@ -1,11 +1,3 @@ -```javascript -// fromLeft :: a -> Either a b -> a -const fromLeft = def => - // The contents of a 'Left' value, or otherwise a default value. - lr => isLeft(lr) ? lr.Left : def; -``` - - ```applescript -- fromLeft :: a -> Either a b -> a on fromLeft(def, lr) @@ -15,4 +7,12 @@ on fromLeft(def, lr) def end if end fromLeft +``` + + +```javascript +// fromLeft :: a -> Either a b -> a +const fromLeft = def => + // The contents of a 'Left' value, or otherwise a default value. + lr => isLeft(lr) ? lr.Left : def; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fromMaybe.md b/MD applescript vs javascript/fromMaybe.md index 8cf75333..45a1461c 100644 --- a/MD applescript vs javascript/fromMaybe.md +++ b/MD applescript vs javascript/fromMaybe.md @@ -1,10 +1,3 @@ -```javascript -// fromMaybe :: a -> Maybe a -> a -const fromMaybe = def => - mb => mb.Nothing ? def : mb.Just; -``` - - ```applescript -- fromMaybe :: a -> Maybe a -> a on fromMaybe(default, mb) @@ -14,4 +7,11 @@ on fromMaybe(default, mb) Just of mb end if end fromMaybe +``` + + +```javascript +// fromMaybe :: a -> Maybe a -> a +const fromMaybe = def => + mb => mb.Nothing ? def : mb.Just; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fromRight.md b/MD applescript vs javascript/fromRight.md index 9755d3d0..c100b1c8 100644 --- a/MD applescript vs javascript/fromRight.md +++ b/MD applescript vs javascript/fromRight.md @@ -1,11 +1,3 @@ -```javascript -// fromRight :: b -> Either a b -> b -const fromRight = def => - // The contents of a 'Right' value or otherwise a default value. - lr => isRight(lr) ? lr.Right : def; -``` - - ```applescript -- fromRight :: b -> Either a b -> b on fromRight(def, lr) @@ -15,4 +7,12 @@ on fromRight(def, lr) def end if end fromRight +``` + + +```javascript +// fromRight :: b -> Either a b -> b +const fromRight = def => + // The contents of a 'Right' value or otherwise a default value. + lr => isRight(lr) ? lr.Right : def; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/fst.md b/MD applescript vs javascript/fst.md index c89903ce..6e84d6b4 100644 --- a/MD applescript vs javascript/fst.md +++ b/MD applescript vs javascript/fst.md @@ -1,11 +1,3 @@ -```javascript -// fst :: (a, b) -> a -const fst = tpl => - // First member of a pair. - tpl[0]; -``` - - ```applescript -- fst :: (a, b) -> a on fst(tpl) @@ -15,4 +7,12 @@ on fst(tpl) item 1 of tpl end if end fst +``` + + +```javascript +// fst :: (a, b) -> a +const fst = tpl => + // First member of a pair. + tpl[0]; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/ft.md b/MD applescript vs javascript/ft.md index fe72ead5..c8acebfe 100644 --- a/MD applescript vs javascript/ft.md +++ b/MD applescript vs javascript/ft.md @@ -1,12 +1,3 @@ -```javascript -// ft :: (Int, Int) -> [Int] -const ft = m => - n => Array.from({ - length: 1 + n - m - }, (_, i) => m + i); -``` - - ```applescript -- Abbreviation for quick testing -- ft :: (Int, Int) -> [Int] @@ -21,4 +12,13 @@ on ft(m, n) return {} end if end ft +``` + + +```javascript +// ft :: (Int, Int) -> [Int] +const ft = m => + n => Array.from({ + length: 1 + n - m + }, (_, i) => m + i); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/gcd.md b/MD applescript vs javascript/gcd.md index ab8a0f59..92ea87a1 100644 --- a/MD applescript vs javascript/gcd.md +++ b/MD applescript vs javascript/gcd.md @@ -1,15 +1,3 @@ -```javascript -// gcd :: Int -> Int -> Int -const gcd = x => - y => { - const - _gcd = (a, b) => (0 === b ? a : _gcd(b, a % b)), - abs = Math.abs; - return _gcd(abs(x), abs(y)); - }; -``` - - ```applescript -- gcd :: Int -> Int -> Int on gcd(a, b) @@ -24,4 +12,16 @@ on gcd(a, b) end repeat return x end gcd +``` + + +```javascript +// gcd :: Int -> Int -> Int +const gcd = x => + y => { + const + _gcd = (a, b) => (0 === b ? a : _gcd(b, a % b)), + abs = Math.abs; + return _gcd(abs(x), abs(y)); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/genericIndexMay.md b/MD applescript vs javascript/genericIndexMay.md index 508b5124..f0a884d3 100644 --- a/MD applescript vs javascript/genericIndexMay.md +++ b/MD applescript vs javascript/genericIndexMay.md @@ -1,12 +1,3 @@ -```javascript -// genericIndexMay :: [a] -> Int -> Maybe a -const genericIndexMay = xs => - i => (i < xs.length && 0 <= i) ? ( - Just(xs[i]) - ) : Nothing(); -``` - - ```applescript -- genericIndexMay :: [a] -> Int -> Maybe a on genericIndexMay(xs, i) @@ -16,4 +7,13 @@ on genericIndexMay(xs, i) Nothing() end if end genericIndexMay +``` + + +```javascript +// genericIndexMay :: [a] -> Int -> Maybe a +const genericIndexMay = xs => + i => (i < xs.length && 0 <= i) ? ( + Just(xs[i]) + ) : Nothing(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/getCurrentDirectory.md b/MD applescript vs javascript/getCurrentDirectory.md index fcf8fbd5..e266992a 100644 --- a/MD applescript vs javascript/getCurrentDirectory.md +++ b/MD applescript vs javascript/getCurrentDirectory.md @@ -1,3 +1,12 @@ +```applescript +-- getCurrentDirectory :: IO FilePath +on getCurrentDirectory() + set ca to current application + ca's NSFileManager's defaultManager()'s currentDirectoryPath as string +end getCurrentDirectory +``` + + ```javascript // getCurrentDirectory :: IO FilePath const getCurrentDirectory = () => @@ -5,13 +14,4 @@ const getCurrentDirectory = () => $.NSFileManager.defaultManager .currentDirectoryPath ); -``` - - -```applescript --- getCurrentDirectory :: IO FilePath -on getCurrentDirectory() - set ca to current application - ca's NSFileManager's defaultManager()'s currentDirectoryPath as string -end getCurrentDirectory ``` \ No newline at end of file diff --git a/MD applescript vs javascript/getDirectoryContents.md b/MD applescript vs javascript/getDirectoryContents.md index fc116040..688915a5 100644 --- a/MD applescript vs javascript/getDirectoryContents.md +++ b/MD applescript vs javascript/getDirectoryContents.md @@ -1,3 +1,15 @@ +```applescript +-- getDirectoryContents :: FilePath -> IO [FilePath] +on getDirectoryContents(strPath) + set ca to current application + (ca's NSFileManager's defaultManager()'s ¬ + contentsOfDirectoryAtPath:(stringByStandardizingPath of (¬ + ca's NSString's stringWithString:(strPath))) ¬ + |error|:(missing value)) as list +end getDirectoryContents +``` + + ```javascript // getDirectoryContents :: FilePath -> IO [FilePath] const getDirectoryContents = fp => @@ -8,16 +20,4 @@ const getDirectoryContents = fp => .stringByStandardizingPath, null ) ); -``` - - -```applescript --- getDirectoryContents :: FilePath -> IO [FilePath] -on getDirectoryContents(strPath) - set ca to current application - (ca's NSFileManager's defaultManager()'s ¬ - contentsOfDirectoryAtPath:(stringByStandardizingPath of (¬ - ca's NSString's stringWithString:(strPath))) ¬ - |error|:(missing value)) as list -end getDirectoryContents ``` \ No newline at end of file diff --git a/MD applescript vs javascript/getDirectoryContentsLR.md b/MD applescript vs javascript/getDirectoryContentsLR.md index ffc77006..0d4a4d2a 100644 --- a/MD applescript vs javascript/getDirectoryContentsLR.md +++ b/MD applescript vs javascript/getDirectoryContentsLR.md @@ -1,20 +1,3 @@ -```javascript -// getDirectoryContentsLR :: FilePath -> Either String IO [FilePath] -const getDirectoryContentsLR = fp => { - const - error = $(), - xs = $.NSFileManager.defaultManager - .contentsOfDirectoryAtPathError( - $(fp).stringByStandardizingPath, - error - ); - return xs.isNil() ? ( - Left(ObjC.unwrap(error.localizedDescription)) - ) : Right(ObjC.deepUnwrap(xs)); -}; -``` - - ```applescript -- getDirectoryContentsLR :: FilePath -> Either String IO [FilePath] on getDirectoryContentsLR(strPath) @@ -29,4 +12,21 @@ on getDirectoryContentsLR(strPath) |Right|(xs as list) end if end getDirectoryContentsLR +``` + + +```javascript +// getDirectoryContentsLR :: FilePath -> Either String IO [FilePath] +const getDirectoryContentsLR = fp => { + const + error = $(), + xs = $.NSFileManager.defaultManager + .contentsOfDirectoryAtPathError( + $(fp).stringByStandardizingPath, + error + ); + return xs.isNil() ? ( + Left(ObjC.unwrap(error.localizedDescription)) + ) : Right(ObjC.deepUnwrap(xs)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/getHomeDirectory.md b/MD applescript vs javascript/getHomeDirectory.md index ee70ade2..636e9e89 100644 --- a/MD applescript vs javascript/getHomeDirectory.md +++ b/MD applescript vs javascript/getHomeDirectory.md @@ -1,13 +1,13 @@ -```javascript -// getHomeDirectory :: IO FilePath -const getHomeDirectory = () => - ObjC.unwrap($.NSHomeDirectory()); -``` - - ```applescript -- getHomeDirectory :: IO FilePath on getHomeDirectory() current application's NSHomeDirectory() as string end getHomeDirectory +``` + + +```javascript +// getHomeDirectory :: IO FilePath +const getHomeDirectory = () => + ObjC.unwrap($.NSHomeDirectory()); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/getTemporaryDirectory.md b/MD applescript vs javascript/getTemporaryDirectory.md index f0b5c85e..7e31656c 100644 --- a/MD applescript vs javascript/getTemporaryDirectory.md +++ b/MD applescript vs javascript/getTemporaryDirectory.md @@ -1,13 +1,13 @@ -```javascript -// getTemporaryDirectory :: IO FilePath -const getTemporaryDirectory = () => - ObjC.unwrap($.NSTemporaryDirectory()); -``` - - ```applescript -- getTemporaryDirectory :: IO FilePath on getTemporaryDirectory() current application's NSTemporaryDirectory() as string end getTemporaryDirectory +``` + + +```javascript +// getTemporaryDirectory :: IO FilePath +const getTemporaryDirectory = () => + ObjC.unwrap($.NSTemporaryDirectory()); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/group.md b/MD applescript vs javascript/group.md index fc39e211..50cc4f52 100644 --- a/MD applescript vs javascript/group.md +++ b/MD applescript vs javascript/group.md @@ -1,3 +1,17 @@ +```applescript +-- group :: Eq a => [a] -> [[a]] +on group(xs) + script eq + on |λ|(a, b) + a = b + end |λ| + end script + + groupBy(eq, xs) +end group +``` + + ```javascript // group :: Eq a => [a] -> [[a]] const group = xs => { @@ -17,18 +31,4 @@ const group = xs => { v.map(x => x.join('')) ) : v; }; -``` - - -```applescript --- group :: Eq a => [a] -> [[a]] -on group(xs) - script eq - on |λ|(a, b) - a = b - end |λ| - end script - - groupBy(eq, xs) -end group ``` \ No newline at end of file diff --git a/MD applescript vs javascript/groupBy.md b/MD applescript vs javascript/groupBy.md index d385d103..cf29c074 100644 --- a/MD applescript vs javascript/groupBy.md +++ b/MD applescript vs javascript/groupBy.md @@ -1,28 +1,3 @@ -```javascript -// groupBy :: (a -> a -> Bool) -> [a] -> [[a]] -const groupBy = fEq => - // Typical usage: groupBy(on(eq)(f), xs) - xs => (ys => 0 < ys.length ? (() => { - const - tpl = ys.slice(1).reduce( - (gw, x) => { - const - gps = gw[0], - wkg = gw[1]; - return fEq(wkg[0])(x) ? ( - Tuple(gps)(wkg.concat([x])) - ) : Tuple(gps.concat([wkg]))([x]); - }, - Tuple([])([ys[0]]) - ), - v = tpl[0].concat([tpl[1]]); - return 'string' !== typeof xs ? ( - v - ) : v.map(x => x.join('')); - })() : [])(list(xs)); -``` - - ```applescript -- Typical usage: groupBy(on(eq, f), xs) -- groupBy :: (a -> a -> Bool) -> [a] -> [[a]] @@ -56,4 +31,29 @@ on groupBy(f, xs) {} end if end groupBy +``` + + +```javascript +// groupBy :: (a -> a -> Bool) -> [a] -> [[a]] +const groupBy = fEq => + // Typical usage: groupBy(on(eq)(f), xs) + xs => (ys => 0 < ys.length ? (() => { + const + tpl = ys.slice(1).reduce( + (gw, x) => { + const + gps = gw[0], + wkg = gw[1]; + return fEq(wkg[0])(x) ? ( + Tuple(gps)(wkg.concat([x])) + ) : Tuple(gps.concat([wkg]))([x]); + }, + Tuple([])([ys[0]]) + ), + v = tpl[0].concat([tpl[1]]); + return 'string' !== typeof xs ? ( + v + ) : v.map(x => x.join('')); + })() : [])(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/groupSortOn.md b/MD applescript vs javascript/groupSortOn.md index b2b48bc3..613fd216 100644 --- a/MD applescript vs javascript/groupSortOn.md +++ b/MD applescript vs javascript/groupSortOn.md @@ -1,15 +1,3 @@ -```javascript -// groupSortOn :: Ord b => (a -> b) -> [a] -> [[a]] -const groupSortOn = f => - compose( - map(map(snd)), - groupBy(on(eq)(fst)), - sortBy(comparing(fst)), - map(fanArrow(f)(identity)) - ); -``` - - ```applescript -- Sort and group a list by comparing the results of a key function -- applied to each element. groupSortOn f is equivalent to @@ -79,4 +67,16 @@ on groupSortOn(f, xs) groupBy(aEq, ((ca's NSArray's arrayWithArray:map(dec, xs))'s ¬ sortedArrayUsingDescriptors:map(descrip, bs)) as list)) end groupSortOn +``` + + +```javascript +// groupSortOn :: Ord b => (a -> b) -> [a] -> [[a]] +const groupSortOn = f => + compose( + map(map(snd)), + groupBy(on(eq)(fst)), + sortBy(comparing(fst)), + map(fanArrow(f)(identity)) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/gt.md b/MD applescript vs javascript/gt.md index 99c636a8..14239007 100644 --- a/MD applescript vs javascript/gt.md +++ b/MD applescript vs javascript/gt.md @@ -1,12 +1,3 @@ -```javascript -// gt :: Ord a => a -> a -> Bool -const gt = x => y => - 'Tuple' === x.type ? ( - x[0] > y[0] - ) : (x > y); -``` - - ```applescript -- gt :: Ord a => a -> a -> Bool on gt(x, y) @@ -17,4 +8,13 @@ on gt(x, y) x > y end if end gt +``` + + +```javascript +// gt :: Ord a => a -> a -> Bool +const gt = x => y => + 'Tuple' === x.type ? ( + x[0] > y[0] + ) : (x > y); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/head.md b/MD applescript vs javascript/head.md index b36ace73..2a437f42 100644 --- a/MD applescript vs javascript/head.md +++ b/MD applescript vs javascript/head.md @@ -1,13 +1,3 @@ -```javascript -// head :: [a] -> a -const head = xs => ( - ys => ys.length ? ( - ys[0] - ) : undefined -)(list(xs)); -``` - - ```applescript -- head :: [a] -> a on head(xs) @@ -17,4 +7,14 @@ on head(xs) item 1 of xs end if end head +``` + + +```javascript +// head :: [a] -> a +const head = xs => ( + ys => ys.length ? ( + ys[0] + ) : undefined +)(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/headMay.md b/MD applescript vs javascript/headMay.md index 380d98d7..eee2a448 100644 --- a/MD applescript vs javascript/headMay.md +++ b/MD applescript vs javascript/headMay.md @@ -1,14 +1,3 @@ -```javascript -// headMay :: [a] -> Maybe a -const headMay = xs => - // Just the first item of xs, or - // Nothing if xs is an empty list. - 0 < xs.length ? ( - Just(xs[0]) - ) : Nothing(); -``` - - ```applescript -- headMay :: [a] -> Maybe a on headMay(xs) @@ -18,4 +7,15 @@ on headMay(xs) Just(item 1 of xs) end if end headMay +``` + + +```javascript +// headMay :: [a] -> Maybe a +const headMay = xs => + // Just the first item of xs, or + // Nothing if xs is an empty list. + 0 < xs.length ? ( + Just(xs[0]) + ) : Nothing(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/identity.md b/MD applescript vs javascript/identity.md index c7b596d5..8d066d4e 100644 --- a/MD applescript vs javascript/identity.md +++ b/MD applescript vs javascript/identity.md @@ -1,15 +1,15 @@ -```javascript -// identity :: a -> a -const identity = x => - // The identity function. - x; -``` - - ```applescript -- identity :: a -> a on identity(x) -- The argument unchanged. x end identity +``` + + +```javascript +// identity :: a -> a +const identity = x => + // The identity function. + x; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/if_.md b/MD applescript vs javascript/if_.md index c20227a9..95596be1 100644 --- a/MD applescript vs javascript/if_.md +++ b/MD applescript vs javascript/if_.md @@ -1,12 +1,3 @@ -```javascript -// if_ :: Bool -> a -> a -> a -const if_ = bln => - x => y => bln ? ( - x - ) : y; -``` - - ```applescript -- if_ :: Bool -> a -> a -> a on if_(bool, x, y) @@ -16,4 +7,13 @@ on if_(bool, x, y) y end if end if_ +``` + + +```javascript +// if_ :: Bool -> a -> a -> a +const if_ = bln => + x => y => bln ? ( + x + ) : y; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/indented.md b/MD applescript vs javascript/indented.md index 637d0e76..7b8511ad 100644 --- a/MD applescript vs javascript/indented.md +++ b/MD applescript vs javascript/indented.md @@ -1,12 +1,3 @@ -```javascript -// indented :: String -> String -> String -const indented = strIndent => - s => s.split(/[\r\n]/).map( - x => '' !== x ? strIndent + x : x - ).join('\n'); -``` - - ```applescript -- indented :: String -> String -> String on indented(strIndent, s) @@ -21,4 +12,13 @@ on indented(strIndent, s) end script unlines(map(result, |lines|(s))) end indented +``` + + +```javascript +// indented :: String -> String -> String +const indented = strIndent => + s => s.split(/[\r\n]/).map( + x => '' !== x ? strIndent + x : x + ).join('\n'); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/index.md b/MD applescript vs javascript/index.md index 866e1ba6..46069068 100644 --- a/MD applescript vs javascript/index.md +++ b/MD applescript vs javascript/index.md @@ -1,18 +1,3 @@ -```javascript -// index (!!) :: [a] -> Int -> Maybe a -// index (!!) :: Generator (Int, a) -> Int -> Maybe a -// index (!!) :: String -> Int -> Maybe Char -const index = xs => - i => { - const s = xs.constructor.constructor.name; - return 'GeneratorFunction' !== s ? (() => { - const v = xs[i]; - return undefined !== v ? Just(v) : Nothing(); - })() : (take(i)(xs), xs.next().value); - }; -``` - - ```applescript -- index (!!) :: [a] -> Int -> Maybe a -- index (!!) :: Gen [a] -> Int -> Maybe a @@ -35,4 +20,19 @@ on |index|(xs, i) end if end if end |index| +``` + + +```javascript +// index (!!) :: [a] -> Int -> Maybe a +// index (!!) :: Generator (Int, a) -> Int -> Maybe a +// index (!!) :: String -> Int -> Maybe Char +const index = xs => + i => { + const s = xs.constructor.constructor.name; + return 'GeneratorFunction' !== s ? (() => { + const v = xs[i]; + return undefined !== v ? Just(v) : Nothing(); + })() : (take(i)(xs), xs.next().value); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/indexOf.md b/MD applescript vs javascript/indexOf.md index c56f885d..7d0f2815 100644 --- a/MD applescript vs javascript/indexOf.md +++ b/MD applescript vs javascript/indexOf.md @@ -1,20 +1,3 @@ -```javascript -// indexOf :: Eq a => [a] -> [a] -> Maybe Int -// indexOf :: String -> String -> Maybe Int -const indexOf = needle => - haystack => 'string' !== typeof haystack ? ( - findIndex(xs => isPrefixOf(needle)(xs))( - tails(haystack) - ) - ) : (() => { - const i = haystack.indexOf(needle); - return -1 !== i ? ( - Just(i) - ) : Nothing(); - })(); -``` - - ```applescript -- indexOf :: Eq a => [a] -> [a] -> Maybe Int -- indexOf :: String -> String -> Maybe Int @@ -35,4 +18,21 @@ on indexOf(pat, src) findIndex(pfx, tails(src)) end if end indexOf +``` + + +```javascript +// indexOf :: Eq a => [a] -> [a] -> Maybe Int +// indexOf :: String -> String -> Maybe Int +const indexOf = needle => + haystack => 'string' !== typeof haystack ? ( + findIndex(xs => isPrefixOf(needle)(xs))( + tails(haystack) + ) + ) : (() => { + const i = haystack.indexOf(needle); + return -1 !== i ? ( + Just(i) + ) : Nothing(); + })(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/init.md b/MD applescript vs javascript/init.md index e729e43a..cfe68345 100644 --- a/MD applescript vs javascript/init.md +++ b/MD applescript vs javascript/init.md @@ -1,14 +1,3 @@ -```javascript -// init :: [a] -> [a] -const init = xs => ( - // All elements of a list except the last. - ys => 0 < ys.length ? ( - ys.slice(0, -1) - ) : undefined -)(list(xs)); -``` - - ```applescript -- init :: [a] -> [a] -- init :: [String] -> [String] @@ -33,4 +22,15 @@ on init(xs) end if end init +``` + + +```javascript +// init :: [a] -> [a] +const init = xs => ( + // All elements of a list except the last. + ys => 0 < ys.length ? ( + ys.slice(0, -1) + ) : undefined +)(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/initMay.md b/MD applescript vs javascript/initMay.md index 5713eb47..d5cccd6d 100644 --- a/MD applescript vs javascript/initMay.md +++ b/MD applescript vs javascript/initMay.md @@ -1,13 +1,3 @@ -```javascript -// initMay :: [a] -> Maybe [a] -const initMay = xs => ( - 0 < ys.length ? ( - Just(ys.slice(0, -1)) - ) : Nothing() -)(list(xs)); -``` - - ```applescript -- initMay :: [a] -> Maybe [a] -- initMay :: [String] -> Maybe [String] @@ -30,4 +20,14 @@ on initMay(xs) Nothing() end if end initMay +``` + + +```javascript +// initMay :: [a] -> Maybe [a] +const initMay = xs => ( + 0 < ys.length ? ( + Just(ys.slice(0, -1)) + ) : Nothing() +)(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/inits.md b/MD applescript vs javascript/inits.md index f3253a14..b5e79748 100644 --- a/MD applescript vs javascript/inits.md +++ b/MD applescript vs javascript/inits.md @@ -1,16 +1,3 @@ -```javascript -// inits :: [a] -> [[a]] -// inits :: String -> [String] -const inits = xs => - // All prefixes of the argument, - // shortest first. - [ - [] - ].concat((list(xs)) - .map((_, i, ys) => ys.slice(0, 1 + i))); -``` - - ```applescript -- inits :: [a] -> [[a]] -- inits :: String -> [String] @@ -33,4 +20,17 @@ on inits(xs) {{}} & map(elemInit, xs) end if end inits +``` + + +```javascript +// inits :: [a] -> [[a]] +// inits :: String -> [String] +const inits = xs => + // All prefixes of the argument, + // shortest first. + [ + [] + ].concat((list(xs)) + .map((_, i, ys) => ys.slice(0, 1 + i))); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/insert.md b/MD applescript vs javascript/insert.md index e9541ce4..330541e0 100644 --- a/MD applescript vs javascript/insert.md +++ b/MD applescript vs javascript/insert.md @@ -1,3 +1,11 @@ +```applescript +-- insert :: Ord a => a -> [a] -> [a] +on insert(x, ys) + insertBy(my compare, x, ys) +end insert +``` + + ```javascript // insert :: Ord a => a -> [a] -> [a] const insert = x => @@ -5,12 +13,4 @@ const insert = x => const [pre, post] = Array.from(break_(y => y >= x)(ys)); return [...pre, x, ...post]; }; -``` - - -```applescript --- insert :: Ord a => a -> [a] -> [a] -on insert(x, ys) - insertBy(my compare, x, ys) -end insert ``` \ No newline at end of file diff --git a/MD applescript vs javascript/insertBy.md b/MD applescript vs javascript/insertBy.md index fa2980fe..8eab966d 100644 --- a/MD applescript vs javascript/insertBy.md +++ b/MD applescript vs javascript/insertBy.md @@ -1,20 +1,3 @@ -```javascript -// insertBy :: (a -> a -> Ordering) -> a -> [a] -> [a] -const insertBy = cmp => - x => xs => { - const go = y => ys => - 0 < ys.length ? ( - 0 < cmp(y)(ys[0]) ? ( - cons(ys[0])( - go(y)(ys.slice(1)) - ) - ) : cons(y)(ys) - ) : [y]; - return go(x)(list(xs)); - }; -``` - - ```applescript -- insertBy :: (a -> a -> Ordering) -> a -> [a] -> [a] on insertBy(cmp, x, ys) @@ -42,4 +25,21 @@ on insertBy(cmp, x, ys) {x} end if end insertBy +``` + + +```javascript +// insertBy :: (a -> a -> Ordering) -> a -> [a] -> [a] +const insertBy = cmp => + x => xs => { + const go = y => ys => + 0 < ys.length ? ( + 0 < cmp(y)(ys[0]) ? ( + cons(ys[0])( + go(y)(ys.slice(1)) + ) + ) : cons(y)(ys) + ) : [y]; + return go(x)(list(xs)); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/insertDict.md b/MD applescript vs javascript/insertDict.md index 65a6a8e3..c8a8cf1c 100644 --- a/MD applescript vs javascript/insertDict.md +++ b/MD applescript vs javascript/insertDict.md @@ -1,12 +1,12 @@ +```applescript +-- insertDict :: String -> a -> Dict -> Dict on insertDict(k, v, rec) tell current application tell dictionaryWithDictionary_(rec) of its NSMutableDictionary its setValue:v forKey:(k as string) it as record end tell end tell end insertDict +``` + + ```javascript // insertDict :: String -> a -> Dict -> Dict const insertDict = k => v => dct => Object.assign({}, dct, { [k]: v }); -``` - - -```applescript --- insertDict :: String -> a -> Dict -> Dict on insertDict(k, v, rec) tell current application tell dictionaryWithDictionary_(rec) of its NSMutableDictionary its setValue:v forKey:(k as string) it as record end tell end tell end insertDict ``` \ No newline at end of file diff --git a/MD applescript vs javascript/intToDigit.md b/MD applescript vs javascript/intToDigit.md index c4300fe7..89e5c14f 100644 --- a/MD applescript vs javascript/intToDigit.md +++ b/MD applescript vs javascript/intToDigit.md @@ -1,12 +1,3 @@ -```javascript -// intToDigit :: Int -> Char -const intToDigit = n => - n >= 0 && n < 16 ? ( - '0123456789ABCDEF'.charAt(n) - ) : '?'; -``` - - ```applescript -- intToDigit :: Int -> Char on intToDigit(n) @@ -16,4 +7,13 @@ on intToDigit(n) "?" end if end intToDigit +``` + + +```javascript +// intToDigit :: Int -> Char +const intToDigit = n => + n >= 0 && n < 16 ? ( + '0123456789ABCDEF'.charAt(n) + ) : '?'; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/intercalate.md b/MD applescript vs javascript/intercalate.md index d19b02cc..27bac91b 100644 --- a/MD applescript vs javascript/intercalate.md +++ b/MD applescript vs javascript/intercalate.md @@ -1,14 +1,3 @@ -```javascript -// intercalate :: [a] -> [[a]] -> [a] -// intercalate :: String -> [String] -> String -const intercalate = sep => xs => - 0 < xs.length && 'string' === typeof sep && - 'string' === typeof xs[0] ? ( - xs.join(sep) - ) : concat(intersperse(sep)(xs)); -``` - - ```applescript -- intercalate :: [a] -> [[a]] -> [a] -- intercalate :: String -> [String] -> String @@ -22,4 +11,15 @@ on intercalate(sep, xs) concat(intersperse(sep, xs)) end if end intercalate +``` + + +```javascript +// intercalate :: [a] -> [[a]] -> [a] +// intercalate :: String -> [String] -> String +const intercalate = sep => xs => + 0 < xs.length && 'string' === typeof sep && + 'string' === typeof xs[0] ? ( + xs.join(sep) + ) : concat(intersperse(sep)(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/intercalateS.md b/MD applescript vs javascript/intercalateS.md index b3efe629..aab2a7e0 100644 --- a/MD applescript vs javascript/intercalateS.md +++ b/MD applescript vs javascript/intercalateS.md @@ -1,12 +1,3 @@ -```javascript -// intercalateS :: String -> [String] -> String -const intercalateS = s => - // The concatenation of xs - // interspersed with copies of s. - xs => xs.join(s); -``` - - ```applescript -- intercalateS :: String -> [String] -> String on intercalateS(delim, xs) @@ -16,4 +7,13 @@ on intercalateS(delim, xs) set my text item delimiters to dlm s end intercalateS +``` + + +```javascript +// intercalateS :: String -> [String] -> String +const intercalateS = s => + // The concatenation of xs + // interspersed with copies of s. + xs => xs.join(s); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/intersect.md b/MD applescript vs javascript/intersect.md index 91214879..b36fb3a9 100644 --- a/MD applescript vs javascript/intersect.md +++ b/MD applescript vs javascript/intersect.md @@ -1,11 +1,3 @@ -```javascript -// intersect :: (Eq a) => [a] -> [a] -> [a] -const intersect = xs => - // The intersection of lists xs and ys. - ys => xs.filter(x => ys.includes(x)); -``` - - ```applescript -- intersect :: (Eq a) => [a] -> [a] -> [a] on intersect(xs, ys) @@ -24,4 +16,12 @@ on intersect(xs, ys) {} end if end intersect +``` + + +```javascript +// intersect :: (Eq a) => [a] -> [a] -> [a] +const intersect = xs => + // The intersection of lists xs and ys. + ys => xs.filter(x => ys.includes(x)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/intersectBy.md b/MD applescript vs javascript/intersectBy.md index cc9f24aa..5391a2e2 100644 --- a/MD applescript vs javascript/intersectBy.md +++ b/MD applescript vs javascript/intersectBy.md @@ -1,17 +1,3 @@ -```javascript -// intersectBy :: (a -> a -> Bool) -> [a] -> [a] -> [a] -const intersectBy = eq => - // The intersection of the lists xs and ys - // in terms of the equality defined by eq. - xs => ys => { - const zs = list(ys); - return list(xs).filter( - x => zs.some(eq(x)) - ); - }; -``` - - ```applescript -- intersectBy :: (a -> a -> Bool) -> [a] -> [a] -> [a] on intersectBy(eq, xs, ys) @@ -28,4 +14,18 @@ on intersectBy(eq, xs, ys) {} end if end intersectBy +``` + + +```javascript +// intersectBy :: (a -> a -> Bool) -> [a] -> [a] -> [a] +const intersectBy = eq => + // The intersection of the lists xs and ys + // in terms of the equality defined by eq. + xs => ys => { + const zs = list(ys); + return list(xs).filter( + x => zs.some(eq(x)) + ); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/intersectListsBy.md b/MD applescript vs javascript/intersectListsBy.md index 46c89a91..dc538292 100644 --- a/MD applescript vs javascript/intersectListsBy.md +++ b/MD applescript vs javascript/intersectListsBy.md @@ -1,12 +1,3 @@ -```javascript -// intersectListsBy :: (a -> a -> Bool) -> [[a]] -> [a] -const intersectListsBy = eq => xs => - foldr1((a => x => intersectBy(eq)(a)(x)))( - list(xs) - ); -``` - - ```applescript -- intersectListsBy :: (a -> a -> Bool) -> [[a]] -> [a] on intersectListsBy(fnEq, xs) @@ -18,4 +9,13 @@ on intersectListsBy(fnEq, xs) end script foldr1(result, xs) end intersectionBy +``` + + +```javascript +// intersectListsBy :: (a -> a -> Bool) -> [[a]] -> [a] +const intersectListsBy = eq => xs => + foldr1((a => x => intersectBy(eq)(a)(x)))( + list(xs) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/intersection.md b/MD applescript vs javascript/intersection.md index eda2d557..ef0d7145 100644 --- a/MD applescript vs javascript/intersection.md +++ b/MD applescript vs javascript/intersection.md @@ -1,10 +1,3 @@ -```javascript -// intersection :: Ord a => Set a -> Set a -> Set a -const intersection = s => s1 => - new Set([...s].filter(x => s1.has(x))); -``` - - ```applescript -- intersection :: Ord a => Set a -> Set a -> Set a on intersection(a, b) @@ -13,4 +6,11 @@ on intersection(a, b) s's intersectSet:(b) return s end intersection +``` + + +```javascript +// intersection :: Ord a => Set a -> Set a -> Set a +const intersection = s => s1 => + new Set([...s].filter(x => s1.has(x))); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/intersperse.md b/MD applescript vs javascript/intersperse.md index 3e35e43d..9c5aa08f 100644 --- a/MD applescript vs javascript/intersperse.md +++ b/MD applescript vs javascript/intersperse.md @@ -1,21 +1,3 @@ -```javascript -// intersperse :: a -> [a] -> [a] -// intersperse :: Char -> String -> String -const intersperse = sep => xs => { - // intersperse(0, [1,2,3]) -> [1, 0, 2, 0, 3] - const bln = 'string' === typeof xs; - return xs.length > 1 ? ( - (bln ? concat : x => x)( - (bln ? ( - xs.split('') - ) : xs) - .slice(1) - .reduce((a, x) => a.concat([sep, x]), [xs[0]]) - )) : xs; -}; -``` - - ```applescript -- intersperse(0, [1,2,3]) -> [1, 0, 2, 0, 3] -- intersperse :: a -> [a] -> [a] @@ -36,4 +18,22 @@ on intersperse(sep, xs) xs end if end intersperse +``` + + +```javascript +// intersperse :: a -> [a] -> [a] +// intersperse :: Char -> String -> String +const intersperse = sep => xs => { + // intersperse(0, [1,2,3]) -> [1, 0, 2, 0, 3] + const bln = 'string' === typeof xs; + return xs.length > 1 ? ( + (bln ? concat : x => x)( + (bln ? ( + xs.split('') + ) : xs) + .slice(1) + .reduce((a, x) => a.concat([sep, x]), [xs[0]]) + )) : xs; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isAlpha.md b/MD applescript vs javascript/isAlpha.md index 7f480171..ac0e4047 100644 --- a/MD applescript vs javascript/isAlpha.md +++ b/MD applescript vs javascript/isAlpha.md @@ -1,10 +1,3 @@ -```javascript -// isAlpha :: Char -> Bool -const isAlpha = c => - /[A-Za-z\u00C0-\u00FF]/.test(c); -``` - - ```applescript -- isAlpha :: Char -> Bool on isAlpha(c) @@ -17,4 +10,11 @@ on isAlpha(c) 0 < (oRgx's numberOfMatchesInString:oString options:0 ¬ range:{location:0, |length|:1}) end isAlpha +``` + + +```javascript +// isAlpha :: Char -> Bool +const isAlpha = c => + /[A-Za-z\u00C0-\u00FF]/.test(c); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isChar.md b/MD applescript vs javascript/isChar.md index 42323980..e340e948 100644 --- a/MD applescript vs javascript/isChar.md +++ b/MD applescript vs javascript/isChar.md @@ -1,13 +1,13 @@ -```javascript -// isChar :: a -> Bool -const isChar = x => - ('string' === typeof x) && (1 === x.length); -``` - - ```applescript -- isChar :: a -> Bool on isChar(x) class of x is string and length of x is 1 end isChar +``` + + +```javascript +// isChar :: a -> Bool +const isChar = x => + ('string' === typeof x) && (1 === x.length); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isDigit.md b/MD applescript vs javascript/isDigit.md index 85527845..a209e7c0 100644 --- a/MD applescript vs javascript/isDigit.md +++ b/MD applescript vs javascript/isDigit.md @@ -1,16 +1,16 @@ -```javascript -// isDigit :: Char -> Bool -const isDigit = c => { - const n = c.codePointAt(0); - return 48 <= n && 57 >= n; -}; -``` - - ```applescript -- isDigit :: Char -> Bool on isDigit(c) set n to (id of c) 48 ≤ n and 57 ≥ n end isDigit +``` + + +```javascript +// isDigit :: Char -> Bool +const isDigit = c => { + const n = c.codePointAt(0); + return 48 <= n && 57 >= n; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isInfixOf.md b/MD applescript vs javascript/isInfixOf.md index 2301cc09..50b7355c 100644 --- a/MD applescript vs javascript/isInfixOf.md +++ b/MD applescript vs javascript/isInfixOf.md @@ -1,3 +1,12 @@ +```applescript +-- isInfixOf :: (Eq a) => [a] -> [a] -> Bool +-- isInfixOf :: String -> String -> Bool +on isInfixOf(needle, haystack) + haystack contains needle +end isInfixOf +``` + + ```javascript // isInfixOf :: (Eq a) => [a] -> [a] -> Bool // isInfixOf :: String -> String -> Bool @@ -10,13 +19,4 @@ const isInfixOf = needle => haystack => ) : false; return go(haystack); })() : haystack.includes(needle); -``` - - -```applescript --- isInfixOf :: (Eq a) => [a] -> [a] -> Bool --- isInfixOf :: String -> String -> Bool -on isInfixOf(needle, haystack) - haystack contains needle -end isInfixOf ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isLeft.md b/MD applescript vs javascript/isLeft.md index 503c79c5..f4ffd5b6 100644 --- a/MD applescript vs javascript/isLeft.md +++ b/MD applescript vs javascript/isLeft.md @@ -1,10 +1,3 @@ -```javascript -// isLeft :: Either a b -> Bool -const isLeft = lr => - ('Either' === lr.type) && (undefined !== lr.Left); -``` - - ```applescript -- isLeft :: Either a b -> Bool on isLeft(x) @@ -13,4 +6,11 @@ on isLeft(x) (dct's objectForKey:"type") as text = "Either" and ¬ (dct's objectForKey:"Right") as list = {missing value} end isLeft +``` + + +```javascript +// isLeft :: Either a b -> Bool +const isLeft = lr => + ('Either' === lr.type) && (undefined !== lr.Left); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isLower.md b/MD applescript vs javascript/isLower.md index 63936088..5410e1fb 100644 --- a/MD applescript vs javascript/isLower.md +++ b/MD applescript vs javascript/isLower.md @@ -1,14 +1,14 @@ -```javascript -// isLower :: Char -> Bool -const isLower = c => - /[a-z]/.test(c); -``` - - ```applescript -- isLower :: Char -> Bool on isLower(c) set d to (id of c) - 97 -- id of "a" d ≥ 0 and d < 26 end isLower +``` + + +```javascript +// isLower :: Char -> Bool +const isLower = c => + /[a-z]/.test(c); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isMaybe.md b/MD applescript vs javascript/isMaybe.md index 38682ba3..be3fe31e 100644 --- a/MD applescript vs javascript/isMaybe.md +++ b/MD applescript vs javascript/isMaybe.md @@ -1,10 +1,3 @@ -```javascript -// isMaybe :: a -> Bool -const isMaybe = x => - 'Maybe' === x.type; -``` - - ```applescript use framework "Foundation" use scripting additions @@ -22,4 +15,11 @@ on isMaybe(x) false end if end isMaybe +``` + + +```javascript +// isMaybe :: a -> Bool +const isMaybe = x => + 'Maybe' === x.type; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isNull.md b/MD applescript vs javascript/isNull.md index b2c6715b..01bae5c6 100644 --- a/MD applescript vs javascript/isNull.md +++ b/MD applescript vs javascript/isNull.md @@ -1,11 +1,3 @@ -```javascript -// isNull :: [a] -> Bool -// isNull :: String -> Bool -const isNull = xs => - 1 > xs.length; -``` - - ```applescript -- isNull :: [a] -> Bool -- isNull :: String -> Bool @@ -16,4 +8,12 @@ on isNull(xs) {} = xs end if end isNull +``` + + +```javascript +// isNull :: [a] -> Bool +// isNull :: String -> Bool +const isNull = xs => + 1 > xs.length; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isPrefixOf.md b/MD applescript vs javascript/isPrefixOf.md index 64fa88a8..f73aab49 100644 --- a/MD applescript vs javascript/isPrefixOf.md +++ b/MD applescript vs javascript/isPrefixOf.md @@ -1,24 +1,3 @@ -```javascript -// isPrefixOf :: [a] -> [a] -> Bool -// isPrefixOf :: String -> String -> Bool -const isPrefixOf = xs => - // True if and only if xs is a prefix of ys. - ys => { - const go = (xs, ys) => { - const intX = xs.length; - return 0 < intX ? ( - ys.length >= intX ? xs[0] === ys[0] && go( - xs.slice(1), ys.slice(1) - ) : false - ) : true; - }; - return 'string' !== typeof xs ? ( - go(xs, ys) - ) : ys.startsWith(xs); - }; -``` - - ```applescript -- isPrefixOf takes two lists or strings and returns -- true if and only if the first is a prefix of the second. @@ -42,4 +21,25 @@ on isPrefixOf(xs, ys) end script go's |λ|(xs, ys) end isPrefixOf +``` + + +```javascript +// isPrefixOf :: [a] -> [a] -> Bool +// isPrefixOf :: String -> String -> Bool +const isPrefixOf = xs => + // True if and only if xs is a prefix of ys. + ys => { + const go = (xs, ys) => { + const intX = xs.length; + return 0 < intX ? ( + ys.length >= intX ? xs[0] === ys[0] && go( + xs.slice(1), ys.slice(1) + ) : false + ) : true; + }; + return 'string' !== typeof xs ? ( + go(xs, ys) + ) : ys.startsWith(xs); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isRight.md b/MD applescript vs javascript/isRight.md index 252aa3d6..f4e1d7c5 100644 --- a/MD applescript vs javascript/isRight.md +++ b/MD applescript vs javascript/isRight.md @@ -1,11 +1,3 @@ -```javascript -// isRight :: Either a b -> Bool -const isRight = lr => - ('undefined' !== typeof lr) && - ('Either' === lr.type) && (undefined !== lr.Right); -``` - - ```applescript -- isRight :: Either a b -> Bool on isRight(x) @@ -14,4 +6,12 @@ on isRight(x) (dct's objectForKey:"type") as text = "Either" and ¬ (dct's objectForKey:"Left") as list = {missing value} end isRight +``` + + +```javascript +// isRight :: Either a b -> Bool +const isRight = lr => + ('undefined' !== typeof lr) && + ('Either' === lr.type) && (undefined !== lr.Right); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isSortedBy.md b/MD applescript vs javascript/isSortedBy.md index 63c1dff1..4ba1d0bc 100644 --- a/MD applescript vs javascript/isSortedBy.md +++ b/MD applescript vs javascript/isSortedBy.md @@ -1,16 +1,3 @@ -```javascript -// isSortedBy :: (a -> a -> Bool) -> [a] -> Bool -const isSortedBy = p => - // True if all adjacent pairs of elements in - // the list return True under the predicate p. - xs => xs.length < 2 || all(x => x < 1)( - zipWith(p)( - xs - )(tail(xs)) - ); -``` - - ```applescript -- The 'isSortedBy' function returns true iff the predicate returns true -- for all adjacent pairs of elements in the list. @@ -23,4 +10,17 @@ on isSortedBy(cmp, xs) end script (length of xs < 2) or all(LE, zipWith(cmp, xs, tail(xs))) end isSortedBy +``` + + +```javascript +// isSortedBy :: (a -> a -> Bool) -> [a] -> Bool +const isSortedBy = p => + // True if all adjacent pairs of elements in + // the list return True under the predicate p. + xs => xs.length < 2 || all(x => x < 1)( + zipWith(p)( + xs + )(tail(xs)) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isSpace.md b/MD applescript vs javascript/isSpace.md index fef74788..2348a934 100644 --- a/MD applescript vs javascript/isSpace.md +++ b/MD applescript vs javascript/isSpace.md @@ -1,15 +1,15 @@ -```javascript -// isSpace :: Char -> Bool -const isSpace = c => - // True if c is a white space character. - /\s/.test(c); -``` - - ```applescript -- isSpace :: Char -> Bool on isSpace(c) set i to id of c 32 = i or (9 ≤ i and 13 ≥ i) end isSpace +``` + + +```javascript +// isSpace :: Char -> Bool +const isSpace = c => + // True if c is a white space character. + /\s/.test(c); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isSubsequenceOf.md b/MD applescript vs javascript/isSubsequenceOf.md index d9a2b695..220e9211 100644 --- a/MD applescript vs javascript/isSubsequenceOf.md +++ b/MD applescript vs javascript/isSubsequenceOf.md @@ -1,26 +1,3 @@ -```javascript -// isSubsequenceOf :: Eq a => [a] -> [a] -> Bool -// isSubsequenceOf :: String -> String -> Bool -const isSubsequenceOf = xs => - // True if xs is a sub-sequence of ys. - ys => { - const go = a => b => - 0 < a.length ? ( - 0 < b.length ? ( - go( - a[0] === b[0] ? ( - a.slice(1) - ) : a - )(b.slice(1)) - ) : false - ) : true; - return go(list(xs))( - list(ys) - ); - }; -``` - - ```applescript -- isSubsequenceOf :: Eq a => [a] -> [a] -> Bool -- isSubsequenceOf :: String -> String -> Bool @@ -49,4 +26,27 @@ on isSubsequenceOf(xs, ys) tell iss to |λ|(xs, ys) end if end isSubsequenceOf +``` + + +```javascript +// isSubsequenceOf :: Eq a => [a] -> [a] -> Bool +// isSubsequenceOf :: String -> String -> Bool +const isSubsequenceOf = xs => + // True if xs is a sub-sequence of ys. + ys => { + const go = a => b => + 0 < a.length ? ( + 0 < b.length ? ( + go( + a[0] === b[0] ? ( + a.slice(1) + ) : a + )(b.slice(1)) + ) : false + ) : true; + return go(list(xs))( + list(ys) + ); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isSubsetOf.md b/MD applescript vs javascript/isSubsetOf.md index d8391729..f696dd44 100644 --- a/MD applescript vs javascript/isSubsetOf.md +++ b/MD applescript vs javascript/isSubsetOf.md @@ -1,3 +1,11 @@ +```applescript +-- isSubsetOf :: Ord a => Set a -> Set a -> Bool +on isSubsetOf(objcSetA, objcSetB) + objcSetA's isSubsetOfSet:(objcSetB) +end isSubsetOf +``` + + ```javascript // isSubsetOf :: Ord a => Set a -> Set a -> Bool const isSubsetOf = a => b => { @@ -6,12 +14,4 @@ const isSubsetOf = a => b => { } return true; }; -``` - - -```applescript --- isSubsetOf :: Ord a => Set a -> Set a -> Bool -on isSubsetOf(objcSetA, objcSetB) - objcSetA's isSubsetOfSet:(objcSetB) -end isSubsetOf ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isSuffixOf.md b/MD applescript vs javascript/isSuffixOf.md index 5bdfbe20..4fe7778d 100644 --- a/MD applescript vs javascript/isSuffixOf.md +++ b/MD applescript vs javascript/isSuffixOf.md @@ -1,15 +1,3 @@ -```javascript -// isSuffixOf :: Eq a => [a] -> [a] -> Bool -// isSuffixOf :: String -> String -> Bool -const isSuffixOf = ns => hs => - 'string' !== typeof hs ? ( - (xs, ys) => bindMay( - dropLengthMaybe(xs)(ys) - )(d => eq(xs)(dropLength(d)(ys))) - )(list(ns), list(hs)) : hs.endsWith(ns); -``` - - ```applescript -- isSuffixOf :: Eq a => [a] -> [a] -> Bool -- isSuffixOf :: String -> String -> Bool @@ -21,4 +9,16 @@ on isSuffixOf(ns, hs) end script bindMay(dropLengthMaybe(ns, hs), go) end isSuffixOf +``` + + +```javascript +// isSuffixOf :: Eq a => [a] -> [a] -> Bool +// isSuffixOf :: String -> String -> Bool +const isSuffixOf = ns => hs => + 'string' !== typeof hs ? ( + (xs, ys) => bindMay( + dropLengthMaybe(xs)(ys) + )(d => eq(xs)(dropLength(d)(ys))) + )(list(ns), list(hs)) : hs.endsWith(ns); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/isUpper.md b/MD applescript vs javascript/isUpper.md index fb28540d..304f8cfb 100644 --- a/MD applescript vs javascript/isUpper.md +++ b/MD applescript vs javascript/isUpper.md @@ -1,15 +1,15 @@ -```javascript -// isUpper :: Char -> Bool -const isUpper = c => - // True if c is an upper case character. - /[A-Z]/.test(c); -``` - - ```applescript -- isUpper :: Char -> Bool on isUpper(c) set d to (id of c) - 65 -- id of "A" d ≥ 0 and d < 26 end isUpper +``` + + +```javascript +// isUpper :: Char -> Bool +const isUpper = c => + // True if c is an upper case character. + /[A-Z]/.test(c); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/iso8601Local.md b/MD applescript vs javascript/iso8601Local.md index 4e2718b4..a25e02f9 100644 --- a/MD applescript vs javascript/iso8601Local.md +++ b/MD applescript vs javascript/iso8601Local.md @@ -1,14 +1,14 @@ -```javascript -// iso8601Local :: Date -> String -const iso8601Local = dte => - new Date(dte - (6E4 * dte.getTimezoneOffset())) - .toISOString(); -``` - - ```applescript -- iso8601Local :: Date -> String on iso8601Local(dte) (dte as «class isot» as string) end iso8601Local +``` + + +```javascript +// iso8601Local :: Date -> String +const iso8601Local = dte => + new Date(dte - (6E4 * dte.getTimezoneOffset())) + .toISOString(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/iterate.md b/MD applescript vs javascript/iterate.md index 2c927fc9..5913ffd0 100644 --- a/MD applescript vs javascript/iterate.md +++ b/MD applescript vs javascript/iterate.md @@ -1,17 +1,3 @@ -```javascript -// iterate :: (a -> a) -> a -> Gen [a] -const iterate = f => - // An infinite list of repeated applications of f to x. - function* (x) { - let v = x; - while (true) { - yield(v); - v = f(v); - } - }; -``` - - ```applescript -- iterate :: (a -> a) -> a -> Gen [a] on iterate(f, x) @@ -28,4 +14,18 @@ on iterate(f, x) end |λ| end script end iterate +``` + + +```javascript +// iterate :: (a -> a) -> a -> Gen [a] +const iterate = f => + // An infinite list of repeated applications of f to x. + function* (x) { + let v = x; + while (true) { + yield(v); + v = f(v); + } + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/iterateUntil.md b/MD applescript vs javascript/iterateUntil.md index 5e3ee94c..41df9426 100644 --- a/MD applescript vs javascript/iterateUntil.md +++ b/MD applescript vs javascript/iterateUntil.md @@ -1,16 +1,3 @@ -```javascript -// iterateUntil :: (a -> Bool) -> (a -> a) -> a -> [a] -const iterateUntil = p => - f => function*(x) { - let v = x; - while (!p(v)) { - yield(v); - v = f(v); - } - }; -``` - - ```applescript -- iterateUntil :: (a -> Bool) -> (a -> a) -> a -> [a] on iterateUntil(p, f, x) @@ -28,4 +15,17 @@ on iterateUntil(p, f, x) end script |λ|(x) of result end iterateUntil +``` + + +```javascript +// iterateUntil :: (a -> Bool) -> (a -> a) -> a -> [a] +const iterateUntil = p => + f => function*(x) { + let v = x; + while (!p(v)) { + yield(v); + v = f(v); + } + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/join.md b/MD applescript vs javascript/join.md index a6f41fae..e5b18f55 100644 --- a/MD applescript vs javascript/join.md +++ b/MD applescript vs javascript/join.md @@ -1,13 +1,13 @@ -```javascript -// join :: Monad m => m (m a) -> m a -const join = x => - bind(x)(identity); -``` - - ```applescript -- join :: Monad m => m (m a) -> m a on join(x) bind(x, my identity) end join +``` + + +```javascript +// join :: Monad m => m (m a) -> m a +const join = x => + bind(x)(identity); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/jsonFromTree.md b/MD applescript vs javascript/jsonFromTree.md index eb2e9c65..5beb91d9 100644 --- a/MD applescript vs javascript/jsonFromTree.md +++ b/MD applescript vs javascript/jsonFromTree.md @@ -1,15 +1,3 @@ -```javascript -// jsonFromTree :: Tree a -> String -const jsonFromTree = tree => { - // A recursive [root, nest] JSON format, - // in which `root` is a value string, and `nest` - // is a possibly empty list of [`root`, `nest`] pairs. - const go = node => [node.root, node.nest.map(go)]; - return JSON.stringify(go(tree)); -}; -``` - - ```applescript -- jsonFromTree :: Tree a -> String on jsonFromTree(tree) @@ -25,4 +13,16 @@ on jsonFromTree(tree) options:0 |error|:(missing value)))) ¬ encoding:(ca's NSUTF8StringEncoding)) as string end jsonFromTree +``` + + +```javascript +// jsonFromTree :: Tree a -> String +const jsonFromTree = tree => { + // A recursive [root, nest] JSON format, + // in which `root` is a value string, and `nest` + // is a possibly empty list of [`root`, `nest`] pairs. + const go = node => [node.root, node.nest.map(go)]; + return JSON.stringify(go(tree)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/jsonLog.md b/MD applescript vs javascript/jsonLog.md index 097139f2..414d1758 100644 --- a/MD applescript vs javascript/jsonLog.md +++ b/MD applescript vs javascript/jsonLog.md @@ -1,3 +1,11 @@ +```applescript +-- jsonLog :: a -> IO () +on jsonLog(e) + log showJSON(e) +end jsonLog +``` + + ```javascript // jsonLog :: a -> IO () const jsonLog = (...args) => @@ -6,12 +14,4 @@ const jsonLog = (...args) => .map(JSON.stringify) .join(' -> ') ); -``` - - -```applescript --- jsonLog :: a -> IO () -on jsonLog(e) - log showJSON(e) -end jsonLog ``` \ No newline at end of file diff --git a/MD applescript vs javascript/jsonParseLR.md b/MD applescript vs javascript/jsonParseLR.md index a9441956..810bec49 100644 --- a/MD applescript vs javascript/jsonParseLR.md +++ b/MD applescript vs javascript/jsonParseLR.md @@ -1,17 +1,3 @@ -```javascript -// jsonParseLR :: String -> Either String a -const jsonParseLR = s => { - try { - return Right(JSON.parse(s)); - } catch (e) { - return Left( - `${e.message} (line:${e.line} col:${e.column})` - ); - } -}; -``` - - ```applescript -- jsonParseLR :: String -> Either String a on jsonParseLR(s) @@ -30,4 +16,18 @@ on jsonParseLR(s) end if end if end jsonParseLR +``` + + +```javascript +// jsonParseLR :: String -> Either String a +const jsonParseLR = s => { + try { + return Right(JSON.parse(s)); + } catch (e) { + return Left( + `${e.message} (line:${e.line} col:${e.column})` + ); + } +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/justifyLeft.md b/MD applescript vs javascript/justifyLeft.md index aca4b551..11e64b8b 100644 --- a/MD applescript vs javascript/justifyLeft.md +++ b/MD applescript vs javascript/justifyLeft.md @@ -1,14 +1,3 @@ -```javascript -// justifyLeft :: Int -> Char -> String -> String -const justifyLeft = n => - // The string s, followed by enough padding (with - // the character c) to reach the string length n. - c => s => n > s.length ? ( - s.padEnd(n, c) - ) : s; -``` - - ```applescript -- justifyLeft :: Int -> Char -> String -> String on justifyLeft(n, cFiller, strText) @@ -18,4 +7,15 @@ on justifyLeft(n, cFiller, strText) strText end if end justifyLeft +``` + + +```javascript +// justifyLeft :: Int -> Char -> String -> String +const justifyLeft = n => + // The string s, followed by enough padding (with + // the character c) to reach the string length n. + c => s => n > s.length ? ( + s.padEnd(n, c) + ) : s; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/justifyRight.md b/MD applescript vs javascript/justifyRight.md index 689a5d36..135c850d 100644 --- a/MD applescript vs javascript/justifyRight.md +++ b/MD applescript vs javascript/justifyRight.md @@ -1,14 +1,3 @@ -```javascript -// justifyRight :: Int -> Char -> String -> String -const justifyRight = n => - // The string s, preceded by enough padding (with - // the character c) to reach the string length n. - c => s => n > s.length ? ( - s.padStart(n, c) - ) : s; -``` - - ```applescript -- justifyRight :: Int -> Char -> String -> String on justifyRight(n, cFiller, strText) @@ -18,4 +7,15 @@ on justifyRight(n, cFiller, strText) strText end if end justifyRight +``` + + +```javascript +// justifyRight :: Int -> Char -> String -> String +const justifyRight = n => + // The string s, preceded by enough padding (with + // the character c) to reach the string length n. + c => s => n > s.length ? ( + s.padStart(n, c) + ) : s; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/keys.md b/MD applescript vs javascript/keys.md index d7edad97..9de9cf5a 100644 --- a/MD applescript vs javascript/keys.md +++ b/MD applescript vs javascript/keys.md @@ -1,12 +1,12 @@ -```javascript -// keys :: Dict -> [String] -const keys = Object.keys; -``` - - ```applescript -- keys :: Dict -> [String] on keys(rec) (current application's NSDictionary's dictionaryWithDictionary:rec)'s allKeys() as list end keys +``` + + +```javascript +// keys :: Dict -> [String] +const keys = Object.keys; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/kleisliCompose (>=>).md b/MD applescript vs javascript/kleisliCompose (>=>).md index 3f4b551e..7b10a4ab 100644 --- a/MD applescript vs javascript/kleisliCompose (>=>).md +++ b/MD applescript vs javascript/kleisliCompose (>=>).md @@ -1,13 +1,3 @@ -```javascript -// kleisliCompose (>=>) :: Monad m => (a -> m b) -> -// (b -> m c) -> (a -> m c) -const kleisliCompose = f => - // Kleisli composition of two functions which - // each lift their values into the same monad. - g => x => bind(f(x))(g); -``` - - ```applescript -- Kleisli composition LR -- kleisliCompose (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c) @@ -18,4 +8,14 @@ on kleisliCompose(f, g) end |λ| end script end kleisliCompose +``` + + +```javascript +// kleisliCompose (>=>) :: Monad m => (a -> m b) -> +// (b -> m c) -> (a -> m c) +const kleisliCompose = f => + // Kleisli composition of two functions which + // each lift their values into the same monad. + g => x => bind(f(x))(g); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/last.md b/MD applescript vs javascript/last.md index c8db4198..699e3fff 100644 --- a/MD applescript vs javascript/last.md +++ b/MD applescript vs javascript/last.md @@ -1,3 +1,11 @@ +```applescript +-- last :: [a] -> a +on |last|(xs) + item -1 of xs +end |last| +``` + + ```javascript // last :: [a] -> a const last = xs => ( @@ -6,12 +14,4 @@ const last = xs => ( ys.slice(-1)[0] ) : undefined )(list(xs)); -``` - - -```applescript --- last :: [a] -> a -on |last|(xs) - item -1 of xs -end |last| ``` \ No newline at end of file diff --git a/MD applescript vs javascript/lastMay.md b/MD applescript vs javascript/lastMay.md index a092725e..9913805a 100644 --- a/MD applescript vs javascript/lastMay.md +++ b/MD applescript vs javascript/lastMay.md @@ -1,13 +1,3 @@ -```javascript -// lastMay :: [a] -> Maybe a -const lastMay = xs => ( - ys => 0 < ys.length ? ( - Just(ys.slice(-1)[0]) - ) : Nothing() -)(list(xs)); -``` - - ```applescript -- lastMay :: [a] -> Maybe a on lastMay(xs) @@ -17,4 +7,14 @@ on lastMay(xs) Nothing() end if end lastMay +``` + + +```javascript +// lastMay :: [a] -> Maybe a +const lastMay = xs => ( + ys => 0 < ys.length ? ( + Just(ys.slice(-1)[0]) + ) : Nothing() +)(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/lcm.md b/MD applescript vs javascript/lcm.md index b467dd42..b5eda3a6 100644 --- a/MD applescript vs javascript/lcm.md +++ b/MD applescript vs javascript/lcm.md @@ -1,14 +1,3 @@ -```javascript -// lcm :: Int -> Int -> Int -const lcm = x => - // The smallest positive integer divisible - // without remainder by both x and y. - y => (x === 0 || y === 0) ? ( - 0 - ) : Math.abs(Math.floor(x / gcd(x)(y)) * y); -``` - - ```applescript -- lcm :: Int -> Int -> Int on lcm(x, y) @@ -18,4 +7,15 @@ on lcm(x, y) abs(floor(x / (gcd(x, y))) * y) end if end lcm +``` + + +```javascript +// lcm :: Int -> Int -> Int +const lcm = x => + // The smallest positive integer divisible + // without remainder by both x and y. + y => (x === 0 || y === 0) ? ( + 0 + ) : Math.abs(Math.floor(x / gcd(x)(y)) * y); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/lefts.md b/MD applescript vs javascript/lefts.md index ee41773a..5744546d 100644 --- a/MD applescript vs javascript/lefts.md +++ b/MD applescript vs javascript/lefts.md @@ -1,14 +1,3 @@ -```javascript -// lefts :: [Either a b] -> [a] -const lefts = xs => - xs.flatMap( - x => ('Either' === x.type) && ( - undefined !== x.Left - ) ? [x.Left] : [] - ); -``` - - ```applescript -- lefts :: [Either a b] -> [a] on lefts(xs) @@ -28,4 +17,15 @@ on lefts(xs) end script concatMap(go, xs) end lefts +``` + + +```javascript +// lefts :: [Either a b] -> [a] +const lefts = xs => + xs.flatMap( + x => ('Either' === x.type) && ( + undefined !== x.Left + ) ? [x.Left] : [] + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/length.md b/MD applescript vs javascript/length.md index bd913aaf..1baae488 100644 --- a/MD applescript vs javascript/length.md +++ b/MD applescript vs javascript/length.md @@ -1,3 +1,16 @@ +```applescript +-- length :: [a] -> Int +on |length|(xs) + set c to class of xs + if list is c or string is c then + length of xs + else + (2 ^ 29 - 1) -- (maxInt - simple proxy for non-finite) + end if +end |length| +``` + + ```javascript // length :: [a] -> Int const length = xs => @@ -9,17 +22,4 @@ const length = xs => .constructor.name ? ( xs.length ) : Infinity; -``` - - -```applescript --- length :: [a] -> Int -on |length|(xs) - set c to class of xs - if list is c or string is c then - length of xs - else - (2 ^ 29 - 1) -- (maxInt - simple proxy for non-finite) - end if -end |length| ``` \ No newline at end of file diff --git a/MD applescript vs javascript/levelNodes.md b/MD applescript vs javascript/levelNodes.md index d81c2a36..f526be35 100644 --- a/MD applescript vs javascript/levelNodes.md +++ b/MD applescript vs javascript/levelNodes.md @@ -1,12 +1,3 @@ -```javascript -// levelNodes :: Tree a -> [[Tree a]] -const levelNodes = tree => - iterateUntil(xs => 1 > xs.length)( - xs => xs.flatMap(x => x.nest) - )([tree]); -``` - - ```applescript -- levelNodes :: Tree a -> [[Tree a]] on levelNodes(tree) @@ -29,4 +20,13 @@ on levelNodes(tree) iterateUntil(p, f, {tree}) end levelNodes +``` + + +```javascript +// levelNodes :: Tree a -> [[Tree a]] +const levelNodes = tree => + iterateUntil(xs => 1 > xs.length)( + xs => xs.flatMap(x => x.nest) + )([tree]); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/levels.md b/MD applescript vs javascript/levels.md index 01ae37f3..410a236f 100644 --- a/MD applescript vs javascript/levels.md +++ b/MD applescript vs javascript/levels.md @@ -1,16 +1,3 @@ -```javascript -// levels :: Tree a -> [[a]] -const levels = tree => - map(map(root))( - takeWhile(xs => 0 < xs.length)( - iterate(concatMap(nest))([ - tree - ]) - ) - ); -``` - - ```applescript -- levels :: Tree a -> [[a]] on levels(tree) @@ -38,4 +25,17 @@ on levels(tree) map(roots, iterateUntil(my isNull, nextLayer, {tree})) end levels +``` + + +```javascript +// levels :: Tree a -> [[a]] +const levels = tree => + map(map(root))( + takeWhile(xs => 0 < xs.length)( + iterate(concatMap(nest))([ + tree + ]) + ) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/liftA2.md b/MD applescript vs javascript/liftA2.md index 74f75a51..a197a1d2 100644 --- a/MD applescript vs javascript/liftA2.md +++ b/MD applescript vs javascript/liftA2.md @@ -1,29 +1,3 @@ -```javascript -// liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c -const liftA2 = f => - // Lift a binary function to actions. - // liftA2 f a b = fmap f a <*> b - a => b => { - const t = typeName(a); - return ( - 'Bottom' !== t ? ( - '(a -> b)' === t ? ( - liftA2Fn - ) : 'Either' === t ? ( - liftA2LR - ) : 'Maybe' === t ? ( - liftA2May - ) : 'Tuple' === t ? ( - liftA2Tuple - ) : 'Node' === t ? ( - liftA2Tree - ) : liftA2List - ) : liftA2List - )(f)(a)(b); - }; -``` - - ```applescript -- Lift a binary function to actions. -- e.g. @@ -51,4 +25,30 @@ on liftA2(f, a, b) end if end if end liftA2 +``` + + +```javascript +// liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c +const liftA2 = f => + // Lift a binary function to actions. + // liftA2 f a b = fmap f a <*> b + a => b => { + const t = typeName(a); + return ( + 'Bottom' !== t ? ( + '(a -> b)' === t ? ( + liftA2Fn + ) : 'Either' === t ? ( + liftA2LR + ) : 'Maybe' === t ? ( + liftA2May + ) : 'Tuple' === t ? ( + liftA2Tuple + ) : 'Node' === t ? ( + liftA2Tree + ) : liftA2List + ) : liftA2List + )(f)(a)(b); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/liftA2Fn.md b/MD applescript vs javascript/liftA2Fn.md index acdef976..7e2b6e4e 100644 --- a/MD applescript vs javascript/liftA2Fn.md +++ b/MD applescript vs javascript/liftA2Fn.md @@ -1,15 +1,3 @@ -```javascript -// liftA2Fn :: (a0 -> b -> c) -> (a -> a0) -> (a -> b) -> a -> c -const liftA2Fn = op => - // Lift a binary function to a composition - // over two other functions. - // liftA2 (*) (+ 2) (+ 3) 7 == 90 - f => g => x => op(f(x))( - g(x) - ); -``` - - ```applescript -- liftA2Fn :: (a0 -> b -> c) -> (a -> a0) -> (a -> b) -> a -> c on liftA2Fn(op, f, g) @@ -25,4 +13,16 @@ on liftA2Fn(op, f, g) end |λ| end script end liftA2Fn +``` + + +```javascript +// liftA2Fn :: (a0 -> b -> c) -> (a -> a0) -> (a -> b) -> a -> c +const liftA2Fn = op => + // Lift a binary function to a composition + // over two other functions. + // liftA2 (*) (+ 2) (+ 3) 7 == 90 + f => g => x => op(f(x))( + g(x) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/liftA2LR.md b/MD applescript vs javascript/liftA2LR.md index c17a8bd4..08a920cc 100644 --- a/MD applescript vs javascript/liftA2LR.md +++ b/MD applescript vs javascript/liftA2LR.md @@ -1,16 +1,3 @@ -```javascript -// liftA2LR :: (a -> b -> c) -> Either d a -> Either d b -> Either d c -const liftA2LR = f => - // The binary function f lifted to a - // function over two Either values. - a => b => bindLR(a)( - x => bindLR(b)( - compose(Right, f(x)) - ) - ); -``` - - ```applescript -- liftA2LR :: (a -> b -> c) -> Either d a -> Either d b -> Either d c on liftA2LR(f, a, b) @@ -29,4 +16,17 @@ on liftA2LR(f, a, b) |Right|(|λ|(x, y) of mReturn(f)) end if end liftA2LR +``` + + +```javascript +// liftA2LR :: (a -> b -> c) -> Either d a -> Either d b -> Either d c +const liftA2LR = f => + // The binary function f lifted to a + // function over two Either values. + a => b => bindLR(a)( + x => bindLR(b)( + compose(Right, f(x)) + ) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/liftA2List.md b/MD applescript vs javascript/liftA2List.md index dbfc0f5f..fe262670 100644 --- a/MD applescript vs javascript/liftA2List.md +++ b/MD applescript vs javascript/liftA2List.md @@ -1,15 +1,3 @@ -```javascript -// liftA2List :: (a -> b -> c) -> [a] -> [b] -> [c] -const liftA2List = f => xs => ys => - // The binary operator f lifted to a function over two - // lists. f applied to each pair of arguments in the - // cartesian product of xs and ys. - list(xs).flatMap( - x => list(ys).map(f(x)) - ); -``` - - ```applescript -- liftA2List :: (a -> b -> c) -> [a] -> [b] -> [c] on liftA2List(f, xs, ys) @@ -26,4 +14,16 @@ on liftA2List(f, xs, ys) end script concatMap(result, xs) end liftA2List +``` + + +```javascript +// liftA2List :: (a -> b -> c) -> [a] -> [b] -> [c] +const liftA2List = f => xs => ys => + // The binary operator f lifted to a function over two + // lists. f applied to each pair of arguments in the + // cartesian product of xs and ys. + list(xs).flatMap( + x => list(ys).map(f(x)) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/liftA2May.md b/MD applescript vs javascript/liftA2May.md index 48e285f9..6b72c71f 100644 --- a/MD applescript vs javascript/liftA2May.md +++ b/MD applescript vs javascript/liftA2May.md @@ -1,14 +1,3 @@ -```javascript -// liftA2May :: (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c -const liftA2May = f => - a => b => a.Nothing ? ( - a - ) : b.Nothing ? ( - b - ) : Just(f(a.Just)(b.Just)); -``` - - ```applescript -- liftA2May :: (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c on liftA2May(f, a, b) @@ -20,4 +9,15 @@ on liftA2May(f, a, b) Just(|λ|(Just of a, Just of b) of mReturn(f)) end if end liftA2May +``` + + +```javascript +// liftA2May :: (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c +const liftA2May = f => + a => b => a.Nothing ? ( + a + ) : b.Nothing ? ( + b + ) : Just(f(a.Just)(b.Just)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/liftA2Tree.md b/MD applescript vs javascript/liftA2Tree.md index 8a7d1394..5dbe89de 100644 --- a/MD applescript vs javascript/liftA2Tree.md +++ b/MD applescript vs javascript/liftA2Tree.md @@ -1,21 +1,3 @@ -```javascript -// liftA2Tree :: (a -> b -> c) -> Tree a -> Tree b -> Tree c -const liftA2Tree = f => - tx => ty => { - const go = t => - Node(f(t.root)(ty.root))( - Boolean(ty.nest) ? ( - ty.nest.map( - fmapTree(f(t.root)) - ) - .concat(t.nest.map(go)) - ) : [] - ); - return go(tx); - }; -``` - - ```applescript -- liftA2Tree :: (a -> b -> c) -> Tree a -> Tree b -> Tree c on liftA2Tree(f, tx, ty) @@ -48,4 +30,22 @@ on liftA2Tree(f, tx, ty) Node(mReturn(f)'s |λ|(root of tx, rootLabel), forest) end liftA2Tree +``` + + +```javascript +// liftA2Tree :: (a -> b -> c) -> Tree a -> Tree b -> Tree c +const liftA2Tree = f => + tx => ty => { + const go = t => + Node(f(t.root)(ty.root))( + Boolean(ty.nest) ? ( + ty.nest.map( + fmapTree(f(t.root)) + ) + .concat(t.nest.map(go)) + ) : [] + ); + return go(tx); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/liftA2Tuple.md b/MD applescript vs javascript/liftA2Tuple.md index f72efdba..ed659bcd 100644 --- a/MD applescript vs javascript/liftA2Tuple.md +++ b/MD applescript vs javascript/liftA2Tuple.md @@ -1,13 +1,3 @@ -```javascript -// liftA2Tuple :: Monoid m => -// (a -> b -> c) -> (m, a) -> (m, b) -> (m, c) -const liftA2Tuple = f => - a => b => Tuple(mappend(a[0])(b[0]))( - f(a[1])(b[1]) - ); -``` - - ```applescript -- liftA2Tuple :: Monoid m => (a -> b -> c) -> (m, a) -> (m, b) -> (m, c) on liftA2Tuple(f, a, b) @@ -20,4 +10,14 @@ on liftA2Tuple(f, a, b) end if Tuple(mappend(|1| of a, b1), mReturn(f)'s |λ|(|2| of a, b2)) end liftA2Tuple +``` + + +```javascript +// liftA2Tuple :: Monoid m => +// (a -> b -> c) -> (m, a) -> (m, b) -> (m, c) +const liftA2Tuple = f => + a => b => Tuple(mappend(a[0])(b[0]))( + f(a[1])(b[1]) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/liftMmay.md b/MD applescript vs javascript/liftMmay.md index 68be6256..480fa694 100644 --- a/MD applescript vs javascript/liftMmay.md +++ b/MD applescript vs javascript/liftMmay.md @@ -1,12 +1,3 @@ -```javascript -// liftMmay :: (a -> b) -> (Maybe a -> Maybe b) -const liftMmay = f => - mb => mb.Nothing ? ( - mb - ) : Just(f(mb.Just)); -``` - - ```applescript -- liftMmay :: (a -> b) -> (Maybe a -> Maybe b) on liftMmay(f) @@ -20,4 +11,13 @@ on liftMmay(f) end |λ| end script end liftMmay +``` + + +```javascript +// liftMmay :: (a -> b) -> (Maybe a -> Maybe b) +const liftMmay = f => + mb => mb.Nothing ? ( + mb + ) : Just(f(mb.Just)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/lines.md b/MD applescript vs javascript/lines.md index 095a92c7..e4579cfa 100644 --- a/MD applescript vs javascript/lines.md +++ b/MD applescript vs javascript/lines.md @@ -1,3 +1,11 @@ +```applescript +-- lines :: String -> [String] +on |lines|(xs) + paragraphs of xs +end |lines| +``` + + ```javascript // lines :: String -> [String] const lines = s => @@ -6,12 +14,4 @@ const lines = s => 0 < s.length ? ( s.split(/[\r\n]/) ) : []; -``` - - -```applescript --- lines :: String -> [String] -on |lines|(xs) - paragraphs of xs -end |lines| ``` \ No newline at end of file diff --git a/MD applescript vs javascript/listDirectory.md b/MD applescript vs javascript/listDirectory.md index 1589817c..27ad53cd 100644 --- a/MD applescript vs javascript/listDirectory.md +++ b/MD applescript vs javascript/listDirectory.md @@ -1,3 +1,14 @@ +```applescript +-- listDirectory :: FilePath -> [FilePath] +on listDirectory(strPath) + set ca to current application + unwrap(ca's NSFileManager's defaultManager()'s ¬ + contentsOfDirectoryAtPath:(unwrap(stringByStandardizingPath of ¬ + wrap(strPath))) |error|:(missing value)) +end listDirectory +``` + + ```javascript // listDirectory :: FilePath -> [FilePath] const listDirectory = fp => @@ -9,15 +20,4 @@ const listDirectory = fp => null )) .map(ObjC.unwrap); -``` - - -```applescript --- listDirectory :: FilePath -> [FilePath] -on listDirectory(strPath) - set ca to current application - unwrap(ca's NSFileManager's defaultManager()'s ¬ - contentsOfDirectoryAtPath:(unwrap(stringByStandardizingPath of ¬ - wrap(strPath))) |error|:(missing value)) -end listDirectory ``` \ No newline at end of file diff --git a/MD applescript vs javascript/listFromMaybe.md b/MD applescript vs javascript/listFromMaybe.md index 2ce65725..94385ea0 100644 --- a/MD applescript vs javascript/listFromMaybe.md +++ b/MD applescript vs javascript/listFromMaybe.md @@ -1,12 +1,3 @@ -```javascript -// listFromMaybe :: Maybe a -> [a] -const listFromMaybe = mb => - // A singleton list derived from a Just value, - // or an empty list derived from Nothing. - mb.Nothing ? [] : [mb.Just]; -``` - - ```applescript -- The listFromMaybe function returns an empty list when given -- Nothing or a singleton list when not given Nothing. @@ -20,4 +11,13 @@ on listFromMaybe(mb) {Just of mb} end if end maybeToList +``` + + +```javascript +// listFromMaybe :: Maybe a -> [a] +const listFromMaybe = mb => + // A singleton list derived from a Just value, + // or an empty list derived from Nothing. + mb.Nothing ? [] : [mb.Just]; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/listFromTree.md b/MD applescript vs javascript/listFromTree.md index e9d7003f..07c61337 100644 --- a/MD applescript vs javascript/listFromTree.md +++ b/MD applescript vs javascript/listFromTree.md @@ -1,15 +1,3 @@ -```javascript -// listFromTree :: Tree a -> [a] -const listFromTree = tree => { - const go = x => [ - x.root, - ...[].concat.apply([], x.nest.map(go)) - ]; - return go(tree); -}; -``` - - ```applescript -- listFromTree :: Tree a -> [a] on listFromTree(tree) @@ -20,4 +8,16 @@ on listFromTree(tree) end script |λ|(tree) of go end listFromTree +``` + + +```javascript +// listFromTree :: Tree a -> [a] +const listFromTree = tree => { + const go = x => [ + x.root, + ...[].concat.apply([], x.nest.map(go)) + ]; + return go(tree); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/listFromTuple.md b/MD applescript vs javascript/listFromTuple.md index cf3c17c1..3a20cd14 100644 --- a/MD applescript vs javascript/listFromTuple.md +++ b/MD applescript vs javascript/listFromTuple.md @@ -1,13 +1,13 @@ -```javascript -// listFromTuple :: (a, a ...) -> [a] -const listFromTuple = tpl => - Array.from(tpl); -``` - - ```applescript -- listFromTuple :: (a, a ...) -> [a] on listFromTuple(tpl) items 2 thru -2 of (tpl as list) end listFromTuple +``` + + +```javascript +// listFromTuple :: (a, a ...) -> [a] +const listFromTuple = tpl => + Array.from(tpl); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/listToMaybe.md b/MD applescript vs javascript/listToMaybe.md index 98310877..e1edeab8 100644 --- a/MD applescript vs javascript/listToMaybe.md +++ b/MD applescript vs javascript/listToMaybe.md @@ -1,13 +1,3 @@ -```javascript -// listToMaybe :: [a] -> Maybe a -const listToMaybe = xs => - // Nothing if xs is empty, or Just the head of xs. - 0 < xs.length ? ( - Just(xs[0]) - ) : Nothing(); -``` - - ```applescript -- The listToMaybe function returns Nothing on -- an empty list or Just the head of the list. @@ -19,4 +9,14 @@ on listToMaybe(xs) Nothing() end if end listToMaybe +``` + + +```javascript +// listToMaybe :: [a] -> Maybe a +const listToMaybe = xs => + // Nothing if xs is empty, or Just the head of xs. + 0 < xs.length ? ( + Just(xs[0]) + ) : Nothing(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/log.md b/MD applescript vs javascript/log.md index 6acb7261..1c03e3a2 100644 --- a/MD applescript vs javascript/log.md +++ b/MD applescript vs javascript/log.md @@ -1,12 +1,12 @@ -```javascript -// log :: Float -> Float -const log = Math.log; -``` - - ```applescript -- log :: Float -> Float on |log|(n) Just of evalJSMay(("Math.log(" & n as string) & ")") end |log| +``` + + +```javascript +// log :: Float -> Float +const log = Math.log; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/lookup.md b/MD applescript vs javascript/lookup.md index 30d8f200..edef64d6 100644 --- a/MD applescript vs javascript/lookup.md +++ b/MD applescript vs javascript/lookup.md @@ -1,14 +1,3 @@ -```javascript -// lookup :: Eq a => a -> Container -> Maybe b -const lookup = k => - // Just of value of the key k in m, - // or Nothing if m does not contain k. - m => (Array.isArray(m) ? ( - lookupTuples - ) : lookupDict)(k)(m); -``` - - ```applescript -- use framework "Foundation" -- use scripting additions @@ -23,4 +12,15 @@ on lookup(k, m) Nothing() end if end lookup +``` + + +```javascript +// lookup :: Eq a => a -> Container -> Maybe b +const lookup = k => + // Just of value of the key k in m, + // or Nothing if m does not contain k. + m => (Array.isArray(m) ? ( + lookupTuples + ) : lookupDict)(k)(m); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/lookupDict.md b/MD applescript vs javascript/lookupDict.md index 843d558e..b1f6aa77 100644 --- a/MD applescript vs javascript/lookupDict.md +++ b/MD applescript vs javascript/lookupDict.md @@ -1,15 +1,3 @@ -```javascript -// lookupDict :: a -> Dict -> Maybe b -const lookupDict = k => - dct => { - const v = dct[k]; - return undefined !== v ? ( - Just(v) - ) : Nothing(); - }; -``` - - ```applescript -- lookupDict :: a -> Dict -> Maybe b on lookupDict(k, dct) @@ -23,4 +11,16 @@ on lookupDict(k, dct) Nothing() end if end lookupDict +``` + + +```javascript +// lookupDict :: a -> Dict -> Maybe b +const lookupDict = k => + dct => { + const v = dct[k]; + return undefined !== v ? ( + Just(v) + ) : Nothing(); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/lookupTuples.md b/MD applescript vs javascript/lookupTuples.md index 6a3558c0..c9c28c8b 100644 --- a/MD applescript vs javascript/lookupTuples.md +++ b/MD applescript vs javascript/lookupTuples.md @@ -1,14 +1,3 @@ -```javascript -// lookupTuples :: Eq a => a -> [(a, b)] -> Maybe b -const lookupTuples = k => - kvs => bindMay( - find(x => k === fst(x))( - kvs - ) - )(x => Just(snd(x))); -``` - - ```applescript -- lookupTuples :: Eq a => a -> [(a, b)] -> Maybe b on lookupTuples(k, xs) @@ -26,4 +15,15 @@ on lookupTuples(k, xs) bindMay(find(keyMatch, xs), harvestMay) end lookupTuples +``` + + +```javascript +// lookupTuples :: Eq a => a -> [(a, b)] -> Maybe b +const lookupTuples = k => + kvs => bindMay( + find(x => k === fst(x))( + kvs + ) + )(x => Just(snd(x))); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/lt.md b/MD applescript vs javascript/lt.md index 1473c072..587f9f44 100644 --- a/MD applescript vs javascript/lt.md +++ b/MD applescript vs javascript/lt.md @@ -1,10 +1,10 @@ +```applescript +-- lt (<) :: Ord a => a -> a -> Bool on lt(x) script on |λ|(y) x < y end |λ| end script end lt +``` + + ```javascript // lt (<) :: Ord a => a -> a -> Bool const lt = a => b => a < b; -``` - - -```applescript --- lt (<) :: Ord a => a -> a -> Bool on lt(x) script on |λ|(y) x < y end |λ| end script end lt ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mReturn.md b/MD applescript vs javascript/mReturn.md index ec52892e..47b74ca5 100644 --- a/MD applescript vs javascript/mReturn.md +++ b/MD applescript vs javascript/mReturn.md @@ -1,15 +1,3 @@ -```javascript -// mReturn :: First-class m => (a -> b) -> m (a -> b) -const mReturn = x => - // Not required in JS, which has first functions by default. - // Included only for comparison with AS, which has to derive - // first class functions by lifting 'handlers' into 'scripts' - // as anonymous |λ|() functions. - // In JS, mReturn is just an alias of identity. - identity(x); -``` - - ```applescript -- mReturn :: First-class m => (a -> b) -> m (a -> b) on mReturn(f) @@ -22,4 +10,16 @@ on mReturn(f) end script end if end mReturn +``` + + +```javascript +// mReturn :: First-class m => (a -> b) -> m (a -> b) +const mReturn = x => + // Not required in JS, which has first functions by default. + // Included only for comparison with AS, which has to derive + // first class functions by lifting 'handlers' into 'scripts' + // as anonymous |λ|() functions. + // In JS, mReturn is just an alias of identity. + identity(x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/map.md b/MD applescript vs javascript/map.md index 8826c0c2..4506c876 100644 --- a/MD applescript vs javascript/map.md +++ b/MD applescript vs javascript/map.md @@ -1,13 +1,3 @@ -```javascript -// map :: (a -> b) -> [a] -> [b] -const map = f => - // The list obtained by applying f - // to each element of xs. - // (The image of xs under f). - xs => [...xs].map(f); -``` - - ```applescript -- map :: (a -> b) -> [a] -> [b] on map(f, xs) @@ -22,4 +12,14 @@ on map(f, xs) return lst end tell end map +``` + + +```javascript +// map :: (a -> b) -> [a] -> [b] +const map = f => + // The list obtained by applying f + // to each element of xs. + // (The image of xs under f). + xs => [...xs].map(f); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mapAccumL.md b/MD applescript vs javascript/mapAccumL.md index 8a444a0c..0be1e094 100644 --- a/MD applescript vs javascript/mapAccumL.md +++ b/MD applescript vs javascript/mapAccumL.md @@ -1,16 +1,3 @@ -```javascript -// mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y]) -const mapAccumL = f => - // A tuple of an accumulation and a list - // obtained by a combined map and fold, - // with accumulation from left to right. - acc => xs => [...xs].reduce((a, x) => { - const pair = f(a[0])(x); - return Tuple(pair[0])(a[1].concat(pair[1])); - }, Tuple(acc)([])); -``` - - ```applescript -- 'The mapAccumL function behaves like a combination of map and foldl; -- it applies a function to each element of a list, passing an @@ -27,4 +14,17 @@ on mapAccumL(f, acc, xs) foldl(result, Tuple(acc, []), xs) end mapAccumL +``` + + +```javascript +// mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y]) +const mapAccumL = f => + // A tuple of an accumulation and a list + // obtained by a combined map and fold, + // with accumulation from left to right. + acc => xs => [...xs].reduce((a, x) => { + const pair = f(a[0])(x); + return Tuple(pair[0])(a[1].concat(pair[1])); + }, Tuple(acc)([])); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mapAccumL_Tree.md b/MD applescript vs javascript/mapAccumL_Tree.md index 0789ad1e..0f5d3c13 100644 --- a/MD applescript vs javascript/mapAccumL_Tree.md +++ b/MD applescript vs javascript/mapAccumL_Tree.md @@ -1,3 +1,19 @@ +```applescript +-- mapAccumL_Tree :: (acc -> x -> (acc, y)) -> acc -> Tree -> (acc, Tree) +on mapAccumL_Tree(f, acc, tree) + script go + property mf : mReturn(f)'s |λ| + on |λ|(a, x) + set pair to f(a, root of x) + set tpl to mapAccumL(go, item 1 of pair, nest of x) + Tuple(item 1 of tpl, Node(item 2 of pair, item 2 of tpl)) + end |λ| + end script + |λ|(acc, tree) of go +end mapAccumL_Tree +``` + + ```javascript // mapAccumL_Tree :: (acc -> x -> (acc, y)) -> // acc -> Tree -> (acc, Tree) @@ -12,20 +28,4 @@ const mapAccumL_Tree = f => { }; return go; }; -``` - - -```applescript --- mapAccumL_Tree :: (acc -> x -> (acc, y)) -> acc -> Tree -> (acc, Tree) -on mapAccumL_Tree(f, acc, tree) - script go - property mf : mReturn(f)'s |λ| - on |λ|(a, x) - set pair to f(a, root of x) - set tpl to mapAccumL(go, item 1 of pair, nest of x) - Tuple(item 1 of tpl, Node(item 2 of pair, item 2 of tpl)) - end |λ| - end script - |λ|(acc, tree) of go -end mapAccumL_Tree ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mapAccumR.md b/MD applescript vs javascript/mapAccumR.md index 9af1ddae..e2fe6834 100644 --- a/MD applescript vs javascript/mapAccumR.md +++ b/MD applescript vs javascript/mapAccumR.md @@ -1,18 +1,3 @@ -```javascript -// mapAccumR :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y]) -const mapAccumR = f => - // A tuple of an accumulation and a list - // obtained by a combined map and fold, - // with accumulation from right to left. - acc => xs => [...xs].reduceRight((a, x) => { - const pair = f(a[0])(x); - return Tuple(pair[0])( - [pair[1]].concat(a[1]) - ); - }, Tuple(acc)([])); -``` - - ```applescript -- 'The mapAccumR function behaves like a combination of map and foldr; -- it applies a function to each element of a list, passing an accumulating @@ -28,4 +13,19 @@ on mapAccumR(f, acc, xs) end script foldr(result, Tuple(acc, []), xs) end mapAccumR +``` + + +```javascript +// mapAccumR :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y]) +const mapAccumR = f => + // A tuple of an accumulation and a list + // obtained by a combined map and fold, + // with accumulation from right to left. + acc => xs => [...xs].reduceRight((a, x) => { + const pair = f(a[0])(x); + return Tuple(pair[0])( + [pair[1]].concat(a[1]) + ); + }, Tuple(acc)([])); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mapKeys.md b/MD applescript vs javascript/mapKeys.md index e444a360..6817a249 100644 --- a/MD applescript vs javascript/mapKeys.md +++ b/MD applescript vs javascript/mapKeys.md @@ -1,17 +1,3 @@ -```javascript -// mapKeys :: (Key -> Key) -> IntMap a -> IntMap a -const mapKeys = f => - // A function mapped over the keys of a record. - dct => mapFromList( - map(kv => [f(read(kv[0]))(kv[1])])( - zip(keys(dct))( - elems(dct) - ) - ) - ); -``` - - ```applescript -- mapKeys :: (Key -> Key) -> IntMap a -> IntMap a on mapKeys(f, dct) @@ -24,4 +10,18 @@ on mapKeys(f, dct) end script map(result, zip(keys(dct), elems(dct))) end mapKeys +``` + + +```javascript +// mapKeys :: (Key -> Key) -> IntMap a -> IntMap a +const mapKeys = f => + // A function mapped over the keys of a record. + dct => mapFromList( + map(kv => [f(read(kv[0]))(kv[1])])( + zip(keys(dct))( + elems(dct) + ) + ) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mapMaybe.md b/MD applescript vs javascript/mapMaybe.md index d7e0f97d..65776a05 100644 --- a/MD applescript vs javascript/mapMaybe.md +++ b/MD applescript vs javascript/mapMaybe.md @@ -1,17 +1,3 @@ -```javascript -// mapMaybe :: (a -> Maybe b) -> [a] -> [b] -const mapMaybe = mf => - // A filtered map, retaining only the contents - // of Just values. (Nothing values discarded). - xs => list(xs).reduce( - (a, x) => maybe(a)( - j => a.concat(j) - )(mf(x)), - [] - ); -``` - - ```applescript -- The mapMaybe function is a version of map which can throw out -- elements. In particular, the functional argument returns @@ -33,4 +19,18 @@ on mapMaybe(mf, xs) end script foldl(result, {}, xs) end mapMaybe +``` + + +```javascript +// mapMaybe :: (a -> Maybe b) -> [a] -> [b] +const mapMaybe = mf => + // A filtered map, retaining only the contents + // of Just values. (Nothing values discarded). + xs => list(xs).reduce( + (a, x) => maybe(a)( + j => a.concat(j) + )(mf(x)), + [] + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mapMaybeGen.md b/MD applescript vs javascript/mapMaybeGen.md index ca25ca2d..1a645435 100644 --- a/MD applescript vs javascript/mapMaybeGen.md +++ b/MD applescript vs javascript/mapMaybeGen.md @@ -1,19 +1,3 @@ -```javascript -// mapMaybeGen :: (a -> Maybe b) -> Gen [a] -> Gen [b] -const mapMaybeGen = mf => - // A filtered map over a generator, returning only the - // contents of Just values. (Nothing values discarded). - function*(gen) { - let v = take(1, gen); - while (0 < v.length) { - let mb = mf(v[0]); - if (!mb.Nothing) yield mb.Just; - v = take(1, gen); - } - }; -``` - - ```applescript -- mapMaybeGen :: (a -> Maybe b) -> Gen [a] -> Gen [b] on mapMaybeGen(mf, gen) @@ -38,4 +22,20 @@ on mapMaybeGen(mf, gen) end |λ| end script end mapMaybeGen +``` + + +```javascript +// mapMaybeGen :: (a -> Maybe b) -> Gen [a] -> Gen [b] +const mapMaybeGen = mf => + // A filtered map over a generator, returning only the + // contents of Just values. (Nothing values discarded). + function*(gen) { + let v = take(1, gen); + while (0 < v.length) { + let mb = mf(v[0]); + if (!mb.Nothing) yield mb.Just; + v = take(1, gen); + } + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mappend (<>).md b/MD applescript vs javascript/mappend (<>).md index 73c6d7c5..1579eafd 100644 --- a/MD applescript vs javascript/mappend (<>).md +++ b/MD applescript vs javascript/mappend (<>).md @@ -1,20 +1,3 @@ -```javascript -// mappend (<>) :: Monoid a => a -> a -> a -const mappend = a => - // Associative operation - // defined for various monoid types. - b => (t => (Boolean(t) ? ( - 'Maybe' === t ? ( - mappendMaybe - ) : mappendTuple - ) : Array.isArray(a) ? ( - append - ) : 'function' === typeof a ? ( - mappendFn - ) : mappendOrd)(a)(b))(a.type); -``` - - ```applescript -- mappend (<>) :: Monoid a => a -> a -> a on mappend(a, b) @@ -38,4 +21,21 @@ on mappend(a, b) a & b end if end mappend +``` + + +```javascript +// mappend (<>) :: Monoid a => a -> a -> a +const mappend = a => + // Associative operation + // defined for various monoid types. + b => (t => (Boolean(t) ? ( + 'Maybe' === t ? ( + mappendMaybe + ) : mappendTuple + ) : Array.isArray(a) ? ( + append + ) : 'function' === typeof a ? ( + mappendFn + ) : mappendOrd)(a)(b))(a.type); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mappendFn.md b/MD applescript vs javascript/mappendFn.md index 4b7d4739..5c273816 100644 --- a/MD applescript vs javascript/mappendFn.md +++ b/MD applescript vs javascript/mappendFn.md @@ -1,12 +1,3 @@ -```javascript -// mappendFn :: Monoid b => (a -> b) -> (a -> b) -> (a -> b) -const mappendFn = f => - g => x => mappend(f(x))( - g(x) - ); -``` - - ```applescript -- mappendFn :: Monoid b => (a -> b) -> (a -> b) -> (a -> b) on mappendFn(f, g) @@ -18,4 +9,13 @@ on mappendFn(f, g) end |λ| end script end mappendFn +``` + + +```javascript +// mappendFn :: Monoid b => (a -> b) -> (a -> b) -> (a -> b) +const mappendFn = f => + g => x => mappend(f(x))( + g(x) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mappendMaybe (<>).md b/MD applescript vs javascript/mappendMaybe (<>).md index 854a1a77..888c4207 100644 --- a/MD applescript vs javascript/mappendMaybe (<>).md +++ b/MD applescript vs javascript/mappendMaybe (<>).md @@ -1,3 +1,17 @@ +```applescript +-- mappendMaybe (<>) :: Maybe a -> Maybe a -> Maybe a +on mappendMaybe(a, b) + if Nothing of a then + b + else if Nothing of b then + a + else + Just(mappend(Just of a, Just of b)) + end if +end mappendMaybe +``` + + ```javascript // mappendMaybe (<>) :: Maybe a -> Maybe a -> Maybe a const mappendMaybe = a => @@ -10,18 +24,4 @@ const mappendMaybe = a => b.Just ) ); -``` - - -```applescript --- mappendMaybe (<>) :: Maybe a -> Maybe a -> Maybe a -on mappendMaybe(a, b) - if Nothing of a then - b - else if Nothing of b then - a - else - Just(mappend(Just of a, Just of b)) - end if -end mappendMaybe ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mappendOrd (<>).md b/MD applescript vs javascript/mappendOrd (<>).md index d7631726..bde4c2e2 100644 --- a/MD applescript vs javascript/mappendOrd (<>).md +++ b/MD applescript vs javascript/mappendOrd (<>).md @@ -1,12 +1,3 @@ -```javascript -// mappendOrd (<>) :: Ordering -> Ordering -> Ordering -const mappendOrd = x => - y => 0 !== x ? ( - x - ) : y; -``` - - ```applescript -- mappendOrd (<>) :: Ordering -> Ordering -> Ordering on mappendOrd(a, b) @@ -16,4 +7,13 @@ on mappendOrd(a, b) b end if end mappendOrd +``` + + +```javascript +// mappendOrd (<>) :: Ordering -> Ordering -> Ordering +const mappendOrd = x => + y => 0 !== x ? ( + x + ) : y; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mappendTuple (<>).md b/MD applescript vs javascript/mappendTuple (<>).md index 97c3de8a..c1be8091 100644 --- a/MD applescript vs javascript/mappendTuple (<>).md +++ b/MD applescript vs javascript/mappendTuple (<>).md @@ -1,3 +1,11 @@ +```applescript +-- mappendTuple (<>) :: (a, b) -> (a, b) -> (a, b) +on mappendTuple(a, b) + Tuple(mappend(|1| of a, |1| of b), mappend(|2| of a, |2| of b)) +end mappendTuple +``` + + ```javascript // mappendTuple (<>) :: (a, b) -> (a, b) -> (a, b) const mappendTuple = t => t1 => @@ -6,12 +14,4 @@ const mappendTuple = t => t1 => )( mappend(t[1])(t1[1]) ); -``` - - -```applescript --- mappendTuple (<>) :: (a, b) -> (a, b) -> (a, b) -on mappendTuple(a, b) - Tuple(mappend(|1| of a, |1| of b), mappend(|2| of a, |2| of b)) -end mappendTuple ``` \ No newline at end of file diff --git a/MD applescript vs javascript/matching.md b/MD applescript vs javascript/matching.md index 7fd54d80..7c5212a7 100644 --- a/MD applescript vs javascript/matching.md +++ b/MD applescript vs javascript/matching.md @@ -1,21 +1,3 @@ -```javascript -// matching :: [a] -> (a -> Int -> [a] -> Bool) -const matching = pat => { - // A sequence-matching function for findIndices etc - // findIndices(matching([2, 3]), [1, 2, 3, 1, 2, 3]) - // -> [1, 4] - const - lng = pat.length, - bln = 0 < lng, - h = bln ? pat[0] : undefined; - return x => i => src => - bln && h == x && eq(pat)( - src.slice(i, lng + i) - ); -}; -``` - - ```applescript -- Returns a sequence-matching function for findIndices etc -- matching :: [a] -> (a -> Int -> [a] -> Bool) @@ -40,4 +22,22 @@ on matching(pat) end |λ| end script end matching +``` + + +```javascript +// matching :: [a] -> (a -> Int -> [a] -> Bool) +const matching = pat => { + // A sequence-matching function for findIndices etc + // findIndices(matching([2, 3]), [1, 2, 3, 1, 2, 3]) + // -> [1, 4] + const + lng = pat.length, + bln = 0 < lng, + h = bln ? pat[0] : undefined; + return x => i => src => + bln && h == x && eq(pat)( + src.slice(i, lng + i) + ); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/max.md b/MD applescript vs javascript/max.md index f3159b78..19fe4ee4 100644 --- a/MD applescript vs javascript/max.md +++ b/MD applescript vs javascript/max.md @@ -1,14 +1,3 @@ -```javascript -// max :: Ord a => a -> a -> a -const max = a => - // b if its greater than a, - // otherwise a. - b => gt(b)(a) ? ( - b - ) : a; -``` - - ```applescript -- max :: Ord a => a -> a -> a on max(x, y) @@ -18,4 +7,15 @@ on max(x, y) y end if end max +``` + + +```javascript +// max :: Ord a => a -> a -> a +const max = a => + // b if its greater than a, + // otherwise a. + b => gt(b)(a) ? ( + b + ) : a; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/maxBound.md b/MD applescript vs javascript/maxBound.md index dc14711f..83d9eb0b 100644 --- a/MD applescript vs javascript/maxBound.md +++ b/MD applescript vs javascript/maxBound.md @@ -1,18 +1,3 @@ -```javascript -// maxBound :: a -> a -const maxBound = x => { - const e = x.enum; - return Boolean(e) ? ( - e[e[x.max]] - ) : { - 'number': Number.MAX_SAFE_INTEGER, - 'string': String.fromCodePoint(0x10FFFF), - 'boolean': true - }[typeof x]; -}; -``` - - ```applescript -- maxBound :: a -> a on maxBound(x) @@ -27,4 +12,19 @@ on maxBound(x) true end if end maxBound +``` + + +```javascript +// maxBound :: a -> a +const maxBound = x => { + const e = x.enum; + return Boolean(e) ? ( + e[e[x.max]] + ) : { + 'number': Number.MAX_SAFE_INTEGER, + 'string': String.fromCodePoint(0x10FFFF), + 'boolean': true + }[typeof x]; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/maximum.md b/MD applescript vs javascript/maximum.md index 8475acd1..d5204b07 100644 --- a/MD applescript vs javascript/maximum.md +++ b/MD applescript vs javascript/maximum.md @@ -1,18 +1,3 @@ -```javascript -// maximum :: Ord a => [a] -> a -const maximum = xs => ( - // The largest value in a non-empty list. - ys => 0 < ys.length ? ( - ys.slice(1).reduce( - (a, y) => y > a ? ( - y - ) : a, ys[0] - ) - ) : undefined -)(list(xs)); -``` - - ```applescript -- maximum :: Ord a => [a] -> a on maximum(xs) @@ -28,4 +13,19 @@ on maximum(xs) foldl(result, missing value, xs) end maximum +``` + + +```javascript +// maximum :: Ord a => [a] -> a +const maximum = xs => ( + // The largest value in a non-empty list. + ys => 0 < ys.length ? ( + ys.slice(1).reduce( + (a, y) => y > a ? ( + y + ) : a, ys[0] + ) + ) : undefined +)(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/maximumBy.md b/MD applescript vs javascript/maximumBy.md index 45830ec3..702f011c 100644 --- a/MD applescript vs javascript/maximumBy.md +++ b/MD applescript vs javascript/maximumBy.md @@ -1,20 +1,3 @@ -```javascript -// maximumBy :: (a -> a -> Ordering) -> [a] -> a -const maximumBy = f => - xs => { - const ys = list(xs); - return 0 < ys.length ? ( - ys.slice(1).reduce( - (a, y) => 0 < f(y)(a) ? ( - y - ) : a, - ys[0] - ) - ) : undefined; - }; -``` - - ```applescript -- maximumBy :: (a -> a -> Ordering) -> [a] -> a on maximumBy(f, xs) @@ -31,4 +14,21 @@ on maximumBy(f, xs) foldl(max, missing value, xs) end maximumBy +``` + + +```javascript +// maximumBy :: (a -> a -> Ordering) -> [a] -> a +const maximumBy = f => + xs => { + const ys = list(xs); + return 0 < ys.length ? ( + ys.slice(1).reduce( + (a, y) => 0 < f(y)(a) ? ( + y + ) : a, + ys[0] + ) + ) : undefined; + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/maximumByMay.md b/MD applescript vs javascript/maximumByMay.md index faf61525..849b155f 100644 --- a/MD applescript vs javascript/maximumByMay.md +++ b/MD applescript vs javascript/maximumByMay.md @@ -1,17 +1,3 @@ -```javascript -// maximumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a -const maximumByMay = f => - xs => ( - ys => ys.length > 0 ? ( - Just(ys.slice(1) - .reduce((a, y) => 0 < f(a)(y) ? ( - a - ) : y, ys[0])) - ) : Nothing() - )(list(xs)); -``` - - ```applescript -- maximumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a on maximumByMay(f, xs) @@ -28,4 +14,18 @@ on maximumByMay(f, xs) foldl1May(max, xs) end maximumByMay +``` + + +```javascript +// maximumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a +const maximumByMay = f => + xs => ( + ys => ys.length > 0 ? ( + Just(ys.slice(1) + .reduce((a, y) => 0 < f(a)(y) ? ( + a + ) : y, ys[0])) + ) : Nothing() + )(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/maximumMay.md b/MD applescript vs javascript/maximumMay.md index 8487493e..47a5d3ca 100644 --- a/MD applescript vs javascript/maximumMay.md +++ b/MD applescript vs javascript/maximumMay.md @@ -1,3 +1,11 @@ +```applescript +-- maximumMay :: Ord a => [a] -> Maybe a +on maximumMay(xs) + foldl1May(max, xs) +end maximumMay +``` + + ```javascript // maximumMay :: Ord a => [a] -> Maybe a const maximumMay = xs => ( @@ -6,12 +14,4 @@ const maximumMay = xs => ( .reduce((a, y) => (y > a ? y : a), ys[0])) ) : Nothing() )(list(xs)); -``` - - -```applescript --- maximumMay :: Ord a => [a] -> Maybe a -on maximumMay(xs) - foldl1May(max, xs) -end maximumMay ``` \ No newline at end of file diff --git a/MD applescript vs javascript/maybe.md b/MD applescript vs javascript/maybe.md index 20cc169a..160a4b72 100644 --- a/MD applescript vs javascript/maybe.md +++ b/MD applescript vs javascript/maybe.md @@ -1,11 +1,3 @@ -```javascript -// maybe :: b -> (a -> b) -> Maybe a -> b -const maybe = v => - // Default value (v) if m is Nothing, or f(m.Just) - f => m => m.Nothing ? v : f(m.Just); -``` - - ```applescript -- maybe :: b -> (a -> b) -> Maybe a -> b on maybe(v, f, mb) @@ -18,4 +10,12 @@ on maybe(v, f, mb) tell mReturn(f) to |λ|(Just of mb) end if end maybe +``` + + +```javascript +// maybe :: b -> (a -> b) -> Maybe a -> b +const maybe = v => + // Default value (v) if m is Nothing, or f(m.Just) + f => m => m.Nothing ? v : f(m.Just); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mean.md b/MD applescript vs javascript/mean.md index 7867aecf..ee925cea 100644 --- a/MD applescript vs javascript/mean.md +++ b/MD applescript vs javascript/mean.md @@ -1,11 +1,3 @@ -```javascript -// mean :: [Num] -> Num -const mean = xs => ( - ys => ys.reduce((a, y) => a + y, 0) / ys.length -)(list(xs)); -``` - - ```applescript -- mean :: [Num] -> Num on mean(xs) @@ -16,4 +8,12 @@ on mean(xs) end script foldl(result, 0, xs) / (length of xs) end mean +``` + + +```javascript +// mean :: [Num] -> Num +const mean = xs => ( + ys => ys.reduce((a, y) => a + y, 0) / ys.length +)(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/min.md b/MD applescript vs javascript/min.md index 2b156333..e77ac05a 100644 --- a/MD applescript vs javascript/min.md +++ b/MD applescript vs javascript/min.md @@ -1,10 +1,3 @@ -```javascript -// min :: Ord a => a -> a -> a -const min = a => - b => b < a ? b : a; -``` - - ```applescript -- min :: Ord a => a -> a -> a on min(x, y) @@ -14,4 +7,11 @@ on min(x, y) x end if end min +``` + + +```javascript +// min :: Ord a => a -> a -> a +const min = a => + b => b < a ? b : a; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/minBound.md b/MD applescript vs javascript/minBound.md index b9665132..963ad577 100644 --- a/MD applescript vs javascript/minBound.md +++ b/MD applescript vs javascript/minBound.md @@ -1,18 +1,3 @@ -```javascript -// minBound :: a -> a -const minBound = x => { - const e = x.enum; - return Boolean(e) ? ( - e[e[0]] - ) : { - 'number': Number.MIN_SAFE_INTEGER, - 'string': String.fromCodePoint(0), - 'boolean': false - }[typeof x]; -}; -``` - - ```applescript -- minBound :: a -> a on minBound(x) @@ -27,4 +12,19 @@ on minBound(x) false end if end minBound +``` + + +```javascript +// minBound :: a -> a +const minBound = x => { + const e = x.enum; + return Boolean(e) ? ( + e[e[0]] + ) : { + 'number': Number.MIN_SAFE_INTEGER, + 'string': String.fromCodePoint(0), + 'boolean': false + }[typeof x]; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/minimum.md b/MD applescript vs javascript/minimum.md index c71e8a8f..e867fc0e 100644 --- a/MD applescript vs javascript/minimum.md +++ b/MD applescript vs javascript/minimum.md @@ -1,15 +1,3 @@ -```javascript -// minimum :: Ord a => [a] -> a -const minimum = xs => ( - // The least value of xs. - ys => 0 < ys.length ? ( - ys.slice(1) - .reduce((a, y) => y < a ? y : a, ys[0]) - ) : undefined -)(list(xs)); -``` - - ```applescript -- minimum :: Ord a => [a] -> a on minimum(xs) @@ -22,4 +10,16 @@ on minimum(xs) end repeat return m end minimum +``` + + +```javascript +// minimum :: Ord a => [a] -> a +const minimum = xs => ( + // The least value of xs. + ys => 0 < ys.length ? ( + ys.slice(1) + .reduce((a, y) => y < a ? y : a, ys[0]) + ) : undefined +)(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/minimumBy.md b/MD applescript vs javascript/minimumBy.md index c5d8778b..982057d2 100644 --- a/MD applescript vs javascript/minimumBy.md +++ b/MD applescript vs javascript/minimumBy.md @@ -1,20 +1,3 @@ -```javascript -// minimumBy :: (a -> a -> Ordering) -> [a] -> a -const minimumBy = f => - xs => { - const ys = list(xs); - return 0 < ys.length ? ( - ys.slice(1).reduce( - (a, y) => 0 > f(y)(a) ? ( - y - ) : a, - ys[0] - ) - ) : undefined; - }; -``` - - ```applescript -- minimumBy :: (a -> a -> Ordering) -> [a] -> a on minimumBy(f, xs) @@ -33,4 +16,21 @@ on minimumBy(f, xs) item 1 of xs end if end minimumBy +``` + + +```javascript +// minimumBy :: (a -> a -> Ordering) -> [a] -> a +const minimumBy = f => + xs => { + const ys = list(xs); + return 0 < ys.length ? ( + ys.slice(1).reduce( + (a, y) => 0 > f(y)(a) ? ( + y + ) : a, + ys[0] + ) + ) : undefined; + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/minimumByMay.md b/MD applescript vs javascript/minimumByMay.md index c32f0f7e..6ac90954 100644 --- a/MD applescript vs javascript/minimumByMay.md +++ b/MD applescript vs javascript/minimumByMay.md @@ -1,15 +1,3 @@ -```javascript -// minimumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a -const minimumByMay = f => - xs => list(xs).reduce((a, x) => - a.Nothing ? Just(x) : ( - f(x)(a.Just) < 0 ? ( - Just(x) - ) : a - ), Nothing()); -``` - - ```applescript -- minimumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a on minimumByMay(f, xs) @@ -28,4 +16,16 @@ on minimumByMay(f, xs) Just(item 1 of xs) end if end minimumByMay +``` + + +```javascript +// minimumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a +const minimumByMay = f => + xs => list(xs).reduce((a, x) => + a.Nothing ? Just(x) : ( + f(x)(a.Just) < 0 ? ( + Just(x) + ) : a + ), Nothing()); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/minimumMay.md b/MD applescript vs javascript/minimumMay.md index b68bda65..b0e69968 100644 --- a/MD applescript vs javascript/minimumMay.md +++ b/MD applescript vs javascript/minimumMay.md @@ -1,15 +1,3 @@ -```javascript -// minimumMay :: [a] -> Maybe a -const minimumMay = xs => ( - ys => 0 < ys.length ? ( - Just(ys.slice(1) - .reduce((a, y) => y < a ? y : a, ys[0]) - ) - ) : Nothing() -)(list(xs)); -``` - - ```applescript -- minimumMay :: [a] -> Maybe a on minimumMay(xs) @@ -27,4 +15,16 @@ on minimumMay(xs) Just(item 1 of xs) end if end minimumMay +``` + + +```javascript +// minimumMay :: [a] -> Maybe a +const minimumMay = xs => ( + ys => 0 < ys.length ? ( + Just(ys.slice(1) + .reduce((a, y) => y < a ? y : a, ys[0]) + ) + ) : Nothing() +)(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mod.md b/MD applescript vs javascript/mod.md index 98e198bd..cdbd0bd1 100644 --- a/MD applescript vs javascript/mod.md +++ b/MD applescript vs javascript/mod.md @@ -1,14 +1,3 @@ -```javascript -// mod :: Int -> Int -> Int -const mod = n => - d => (n % d) + ( - signum(n) === signum(-d) ? ( - d - ) : 0 - ); -``` - - ```applescript -- mod :: Int -> Int -> Int on |mod|(n, d) @@ -18,4 +7,15 @@ on |mod|(n, d) (n mod d) end if end |mod| +``` + + +```javascript +// mod :: Int -> Int -> Int +const mod = n => + d => (n % d) + ( + signum(n) === signum(-d) ? ( + d + ) : 0 + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/modificationTime.md b/MD applescript vs javascript/modificationTime.md index 26c4692f..dbf1167d 100644 --- a/MD applescript vs javascript/modificationTime.md +++ b/MD applescript vs javascript/modificationTime.md @@ -1,12 +1,3 @@ -```javascript -// modificationTime :: FilePath -> Either String Date -const modificationTime = fp => - bindLR(fileStatus(fp))( - dct => Right(ObjC.unwrap(dct.NSFileModificationDate)) - ); -``` - - ```applescript -- modificationTime :: FilePath -> Either String Date on modificationTime(fp) @@ -17,4 +8,13 @@ on modificationTime(fp) end script bindLR(my fileStatus(fp), fs) end modificationTime +``` + + +```javascript +// modificationTime :: FilePath -> Either String Date +const modificationTime = fp => + bindLR(fileStatus(fp))( + dct => Right(ObjC.unwrap(dct.NSFileModificationDate)) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/mul.md b/MD applescript vs javascript/mul.md index 4695de39..950a085f 100644 --- a/MD applescript vs javascript/mul.md +++ b/MD applescript vs javascript/mul.md @@ -1,10 +1,3 @@ -```javascript -// mul (*) :: Num a => a -> a -> a -const mul = a => - b => a * b; -``` - - ```applescript -- mul (*) :: Num a => a -> a -> a on mul(a) @@ -15,4 +8,11 @@ on mul(a) end |λ| end script end mul +``` + + +```javascript +// mul (*) :: Num a => a -> a -> a +const mul = a => + b => a * b; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/ne.md b/MD applescript vs javascript/ne.md index ba69e1f5..dcf3823e 100644 --- a/MD applescript vs javascript/ne.md +++ b/MD applescript vs javascript/ne.md @@ -1,10 +1,3 @@ -```javascript -// ne :: a -> a -> Bool -const ne = a => - b => a !== b; -``` - - ```applescript -- ne :: a -> a -> Bool on ne(a) @@ -14,4 +7,11 @@ on ne(a) end |λ| end script end ne +``` + + +```javascript +// ne :: a -> a -> Bool +const ne = a => + b => a !== b; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/negate.md b/MD applescript vs javascript/negate.md index 51fde537..6764092a 100644 --- a/MD applescript vs javascript/negate.md +++ b/MD applescript vs javascript/negate.md @@ -1,13 +1,13 @@ -```javascript -// negate :: Num -> Num -const negate = n => - -n; -``` - - ```applescript -- negate :: Num -> Num on |negate|(n) -n end |negate| +``` + + +```javascript +// negate :: Num -> Num +const negate = n => + -n; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/nest.md b/MD applescript vs javascript/nest.md index 4ab8e24d..2d704927 100644 --- a/MD applescript vs javascript/nest.md +++ b/MD applescript vs javascript/nest.md @@ -1,3 +1,11 @@ +```applescript +-- nest :: Tree a -> [a] +on nest(oTree) + nest of oTree +end nest +``` + + ```javascript // nest :: Tree a -> [a] const nest = tree => { @@ -10,12 +18,4 @@ const nest = tree => { xs ) : xs(root(x)); }; -``` - - -```applescript --- nest :: Tree a -> [a] -on nest(oTree) - nest of oTree -end nest ``` \ No newline at end of file diff --git a/MD applescript vs javascript/newUUID.md b/MD applescript vs javascript/newUUID.md index 711bd4ee..d6ae590f 100644 --- a/MD applescript vs javascript/newUUID.md +++ b/MD applescript vs javascript/newUUID.md @@ -1,10 +1,3 @@ -```javascript -// newUUID :: () -> IO UUID String -const newUUID = () => - ObjC.unwrap($.NSUUID.UUID.UUIDString); -``` - - ```applescript -- use framework "Foundation" -- use scripting additions @@ -12,4 +5,11 @@ const newUUID = () => on newUUID() current application's NSUUID's UUID's UUIDString as string end newUUID +``` + + +```javascript +// newUUID :: () -> IO UUID String +const newUUID = () => + ObjC.unwrap($.NSUUID.UUID.UUIDString); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/not.md b/MD applescript vs javascript/not.md index 5d800975..a4a083cc 100644 --- a/MD applescript vs javascript/not.md +++ b/MD applescript vs javascript/not.md @@ -1,14 +1,14 @@ -```javascript -// not :: Bool -> Bool -const not = b => - !b; -``` - - ```applescript -- not :: Bool -> Bool on |not|(p) -- `not` as a composable and mappable function. not p end |not| +``` + + +```javascript +// not :: Bool -> Bool +const not = b => + !b; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/notElem.md b/MD applescript vs javascript/notElem.md index 29919f73..4ca9a855 100644 --- a/MD applescript vs javascript/notElem.md +++ b/MD applescript vs javascript/notElem.md @@ -1,13 +1,13 @@ -```javascript -// notElem :: Eq a => a -> [a] -> Bool -const notElem = x => - xs => !xs.includes(x); -``` - - ```applescript -- notElem :: Eq a => a -> [a] -> Bool on notElem(x, xs) xs does not contain x end notElem +``` + + +```javascript +// notElem :: Eq a => a -> [a] -> Bool +const notElem = x => + xs => !xs.includes(x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/nub.md b/MD applescript vs javascript/nub.md index 332fe9db..210e4e6a 100644 --- a/MD applescript vs javascript/nub.md +++ b/MD applescript vs javascript/nub.md @@ -1,13 +1,13 @@ -```javascript -// nub :: [a] -> [a] -const nub = xs => - nubBy(eq)(xs); -``` - - ```applescript -- nub :: [a] -> [a] on nub(xs) nubBy(eq, xs) end nub +``` + + +```javascript +// nub :: [a] -> [a] +const nub = xs => + nubBy(eq)(xs); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/nubBy.md b/MD applescript vs javascript/nubBy.md index 8735cc60..8d5fbc4f 100644 --- a/MD applescript vs javascript/nubBy.md +++ b/MD applescript vs javascript/nubBy.md @@ -1,19 +1,3 @@ -```javascript -// nubBy :: (a -> a -> Bool) -> [a] -> [a] -const nubBy = fEq => { - const go = xs => 0 < xs.length ? (() => { - const x = xs[0]; - return [x].concat( - go(xs.slice(1) - .filter(y => !fEq(x)(y)) - ) - ); - })() : []; - return compose(go, list); -}; -``` - - ```applescript -- nubBy :: (a -> a -> Bool) -> [a] -> [a] on nubBy(f, xs) @@ -43,4 +27,20 @@ on nubBy(f, xs) go's |λ|(xs) end nubBy +``` + + +```javascript +// nubBy :: (a -> a -> Bool) -> [a] -> [a] +const nubBy = fEq => { + const go = xs => 0 < xs.length ? (() => { + const x = xs[0]; + return [x].concat( + go(xs.slice(1) + .filter(y => !fEq(x)(y)) + ) + ); + })() : []; + return compose(go, list); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/odd.md b/MD applescript vs javascript/odd.md index 0d957403..93927e72 100644 --- a/MD applescript vs javascript/odd.md +++ b/MD applescript vs javascript/odd.md @@ -1,13 +1,13 @@ -```javascript -// odd :: Int -> Bool -const odd = n => - !even(n); -``` - - ```applescript -- odd :: Int -> Bool on odd(x) not even(x) end odd +``` + + +```javascript +// odd :: Int -> Bool +const odd = n => + !even(n); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/on.md b/MD applescript vs javascript/on.md index b6d26047..06696558 100644 --- a/MD applescript vs javascript/on.md +++ b/MD applescript vs javascript/on.md @@ -1,11 +1,3 @@ -```javascript -// on :: (b -> b -> c) -> (a -> b) -> a -> a -> c -const on = f => - // e.g. groupBy(on(eq)(length)) - g => a => b => f(g(a))(g(b)); -``` - - ```applescript -- e.g. sortBy(|on|(compare, |length|), ["epsilon", "mu", "gamma", "beta"]) -- on :: (b -> b -> c) -> (a -> b) -> a -> a -> c @@ -17,4 +9,12 @@ on |on|(f, g) end |λ| end script end |on| +``` + + +```javascript +// on :: (b -> b -> c) -> (a -> b) -> a -> a -> c +const on = f => + // e.g. groupBy(on(eq)(length)) + g => a => b => f(g(a))(g(b)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/or.md b/MD applescript vs javascript/or.md index 3ecaac02..ae3f2770 100644 --- a/MD applescript vs javascript/or.md +++ b/MD applescript vs javascript/or.md @@ -1,10 +1,3 @@ -```javascript -// or :: [Bool] -> Bool -const or = xs => - xs.some(Boolean); -``` - - ```applescript -- or :: [Bool] -> Bool on |or|(ps) @@ -13,4 +6,11 @@ on |or|(ps) end repeat return false end |or| +``` + + +```javascript +// or :: [Bool] -> Bool +const or = xs => + xs.some(Boolean); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/ord.md b/MD applescript vs javascript/ord.md index 0b87faab..d87baeaa 100644 --- a/MD applescript vs javascript/ord.md +++ b/MD applescript vs javascript/ord.md @@ -1,14 +1,14 @@ -```javascript -// ord :: Char -> Int -const ord = c => - // Unicode ordinal value of the character. - c.codePointAt(0); -``` - - ```applescript -- ord :: Char -> Int on ord(c) id of c end ord +``` + + +```javascript +// ord :: Char -> Int +const ord = c => + // Unicode ordinal value of the character. + c.codePointAt(0); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/ordering.md b/MD applescript vs javascript/ordering.md index 5769de93..f4d09002 100644 --- a/MD applescript vs javascript/ordering.md +++ b/MD applescript vs javascript/ordering.md @@ -1,3 +1,11 @@ +```applescript +-- ordering :: () -> Ordering +on ordering() + enumFromPairs("Ordering", {{"LT", -1}, {"EQ", 0}, {"GT", 1}}) +end ordering +``` + + ```javascript // ordering :: () -> Ordering const @@ -8,12 +16,4 @@ const LT = ordering.LT, EQ = ordering.EQ, GT = ordering.GT; -``` - - -```applescript --- ordering :: () -> Ordering -on ordering() - enumFromPairs("Ordering", {{"LT", -1}, {"EQ", 0}, {"GT", 1}}) -end ordering ``` \ No newline at end of file diff --git a/MD applescript vs javascript/outdented.md b/MD applescript vs javascript/outdented.md index 2079b488..0ce1f75d 100644 --- a/MD applescript vs javascript/outdented.md +++ b/MD applescript vs javascript/outdented.md @@ -1,21 +1,3 @@ -```javascript -// outdented :: String -> String -const outdented = s => { - // All lines in the string outdented by the same amount - // (just enough to ensure that the least indented lines - // have no remaining indent) - // All relative indents are left unchanged - const - rgx = /^ */, // Leading space characters. - xs = lines(s), - n = length(minimumBy(comparing(length))( - xs.map(txt => rgx.exec(txt)[0]) - )); - return unlines(map(drop(n))(xs)); -}; -``` - - ```applescript -- All lines in the string outdented by the same amount -- (just enough to ensure that the least indented lines @@ -41,4 +23,22 @@ on outdented(s) unlines(map(|λ|(n) of curry(drop), xs)) end if end outdented +``` + + +```javascript +// outdented :: String -> String +const outdented = s => { + // All lines in the string outdented by the same amount + // (just enough to ensure that the least indented lines + // have no remaining indent) + // All relative indents are left unchanged + const + rgx = /^ */, // Leading space characters. + xs = lines(s), + n = length(minimumBy(comparing(length))( + xs.map(txt => rgx.exec(txt)[0]) + )); + return unlines(map(drop(n))(xs)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/partition.md b/MD applescript vs javascript/partition.md index 3cfc5a26..574e41a6 100644 --- a/MD applescript vs javascript/partition.md +++ b/MD applescript vs javascript/partition.md @@ -1,17 +1,3 @@ -```javascript -// partition :: (a -> Bool) -> [a] -> ([a], [a]) -const partition = p => - // A tuple of two lists - those elements in - // xs which match p, and those which don't. - xs => list(xs).reduce( - (a, x) => p(x) ? ( - Tuple(a[0].concat(x))(a[1]) - ) : Tuple(a[0])(a[1].concat(x)), - Tuple([])([]) - ); -``` - - ```applescript -- partition :: (a -> Bool) -> [a] -> ([a], [a]) on partition(f, xs) @@ -29,4 +15,18 @@ on partition(f, xs) end tell Tuple(ys, zs) end partition +``` + + +```javascript +// partition :: (a -> Bool) -> [a] -> ([a], [a]) +const partition = p => + // A tuple of two lists - those elements in + // xs which match p, and those which don't. + xs => list(xs).reduce( + (a, x) => p(x) ? ( + Tuple(a[0].concat(x))(a[1]) + ) : Tuple(a[0])(a[1].concat(x)), + Tuple([])([]) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/partitionEithers.md b/MD applescript vs javascript/partitionEithers.md index 08f6274b..90d4e9b8 100644 --- a/MD applescript vs javascript/partitionEithers.md +++ b/MD applescript vs javascript/partitionEithers.md @@ -1,15 +1,3 @@ -```javascript -// partitionEithers :: [Either a b] -> ([a],[b]) -const partitionEithers = xs => - xs.reduce( - (a, x) => undefined !== x.Left ? ( - Tuple(a[0].concat(x.Left))(a[1]) - ) : Tuple(a[0])(a[1].concat(x.Right)), - Tuple([])([]) - ); -``` - - ```applescript -- partitionEithers :: [Either a b] -> ([a],[b]) on partitionEithers(xs) @@ -24,4 +12,16 @@ on partitionEithers(xs) end repeat Tuple(ys, zs) end partitionEithers +``` + + +```javascript +// partitionEithers :: [Either a b] -> ([a],[b]) +const partitionEithers = xs => + xs.reduce( + (a, x) => undefined !== x.Left ? ( + Tuple(a[0].concat(x.Left))(a[1]) + ) : Tuple(a[0])(a[1].concat(x.Right)), + Tuple([])([]) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/permutations.md b/MD applescript vs javascript/permutations.md index 2a6f1979..a8ec3a2d 100644 --- a/MD applescript vs javascript/permutations.md +++ b/MD applescript vs javascript/permutations.md @@ -1,21 +1,3 @@ -```javascript -// permutations :: [a] -> [[a]] -const permutations = xs => ( - ys => ys.reduceRight( - (a, y) => a.flatMap( - ys => Array.from({ - length: 1 + ys.length - }, (_, i) => i) - .map(n => ys.slice(0, n) - .concat(y) - .concat(ys.slice(n)) - ) - ),[[]] - ) -)(list(xs)); -``` - - ```applescript -- permutations :: [a] -> [[a]] on permutations(xs) @@ -40,4 +22,22 @@ on permutations(xs) end script foldr(go, {{}}, xs) end permutations +``` + + +```javascript +// permutations :: [a] -> [[a]] +const permutations = xs => ( + ys => ys.reduceRight( + (a, y) => a.flatMap( + ys => Array.from({ + length: 1 + ys.length + }, (_, i) => i) + .map(n => ys.slice(0, n) + .concat(y) + .concat(ys.slice(n)) + ) + ),[[]] + ) +)(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/pi.md b/MD applescript vs javascript/pi.md index 11a040f1..548d4625 100644 --- a/MD applescript vs javascript/pi.md +++ b/MD applescript vs javascript/pi.md @@ -1,12 +1,12 @@ -```javascript -// pi :: Float -const pi = Math.PI; -``` - - ```applescript -- pi :: Float on |pi|() pi end |pi| +``` + + +```javascript +// pi :: Float +const pi = Math.PI; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/plus.md b/MD applescript vs javascript/plus.md index 65bd2f62..7b63eb97 100644 --- a/MD applescript vs javascript/plus.md +++ b/MD applescript vs javascript/plus.md @@ -1,12 +1,12 @@ -```javascript -// plus :: Num -> Num -> Num -const plus = a => b => a + b; -``` - - ```applescript -- plus :: Num -> Num -> Num on plus(a, b) a + b end plus +``` + + +```javascript +// plus :: Num -> Num -> Num +const plus = a => b => a + b; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/postorder.md b/MD applescript vs javascript/postorder.md index 624be687..e4bcb0a3 100644 --- a/MD applescript vs javascript/postorder.md +++ b/MD applescript vs javascript/postorder.md @@ -1,15 +1,3 @@ -```javascript -// postorder :: Tree a -> [a] -const postorder = t => { - // List of root elements of tree flattened - // bottom-up into a postorder list. - const go = (xs, x) => - nest(x).reduce(go, xs).concat(root(x)); - return go([], t); -}; -``` - - ```applescript -- Root elements of tree flattened bottom-up -- into a postorder list. @@ -22,4 +10,16 @@ on postorder(node) end script go's |λ|({}, node) end postorder +``` + + +```javascript +// postorder :: Tree a -> [a] +const postorder = t => { + // List of root elements of tree flattened + // bottom-up into a postorder list. + const go = (xs, x) => + nest(x).reduce(go, xs).concat(root(x)); + return go([], t); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/pred.md b/MD applescript vs javascript/pred.md index 63953d1e..568800aa 100644 --- a/MD applescript vs javascript/pred.md +++ b/MD applescript vs javascript/pred.md @@ -1,3 +1,15 @@ +```applescript +-- pred :: Enum a => a -> a +on pred(x) + if isChar(x) then + chr(ord(x) - 1) + else + (-1) + x + end if +end pred +``` + + ```javascript // pred :: Enum a => a -> a const pred = x => { @@ -11,16 +23,4 @@ const pred = x => { x - 1 ) : Error('succ :: Num out of range.'); }; -``` - - -```applescript --- pred :: Enum a => a -> a -on pred(x) - if isChar(x) then - chr(ord(x) - 1) - else - (-1) + x - end if -end pred ``` \ No newline at end of file diff --git a/MD applescript vs javascript/predMay.md b/MD applescript vs javascript/predMay.md index 63c8c44d..d6644d58 100644 --- a/MD applescript vs javascript/predMay.md +++ b/MD applescript vs javascript/predMay.md @@ -1,3 +1,15 @@ +```applescript +-- predMay :: Enum a => a -> Maybe a +on predMay(x) + if x is minBound(x) then + Nothing() + else + Just(toEnum(x)'s |λ|(fromEnum(x) - 1)) + end if +end predMay +``` + + ```javascript // predMay :: Enum a => a -> Maybe a const predMay = x => { @@ -11,16 +23,4 @@ const predMay = x => { Just(x - 1) ) : Nothing(); }; -``` - - -```applescript --- predMay :: Enum a => a -> Maybe a -on predMay(x) - if x is minBound(x) then - Nothing() - else - Just(toEnum(x)'s |λ|(fromEnum(x) - 1)) - end if -end predMay ``` \ No newline at end of file diff --git a/MD applescript vs javascript/print.md b/MD applescript vs javascript/print.md index c06f1d62..917b23f5 100644 --- a/MD applescript vs javascript/print.md +++ b/MD applescript vs javascript/print.md @@ -1,3 +1,12 @@ +```applescript +-- print :: a -> IO () +on print (x) + log x + return x +end print +``` + + ```javascript // print :: a -> IO () const print = x => { @@ -15,13 +24,4 @@ const print = x => { ) ); }; -``` - - -```applescript --- print :: a -> IO () -on print (x) - log x - return x -end print ``` \ No newline at end of file diff --git a/MD applescript vs javascript/product.md b/MD applescript vs javascript/product.md index 9f12c083..d56f1aa7 100644 --- a/MD applescript vs javascript/product.md +++ b/MD applescript vs javascript/product.md @@ -1,10 +1,3 @@ -```javascript -// product :: [Num] -> Num -const product = xs => - list(xs).reduce((a, x) => a * x, 1); -``` - - ```applescript -- product :: [Num] -> Num on product(xs) @@ -16,4 +9,11 @@ on product(xs) foldl(multiply, 1, xs) end product +``` + + +```javascript +// product :: [Num] -> Num +const product = xs => + list(xs).reduce((a, x) => a * x, 1); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/properFracRatio.md b/MD applescript vs javascript/properFracRatio.md index fbd91b5d..fa025ea5 100644 --- a/MD applescript vs javascript/properFracRatio.md +++ b/MD applescript vs javascript/properFracRatio.md @@ -1,12 +1,3 @@ -```javascript -// properFracRatio :: Ratio -> (Int, Ratio) -const properFracRatio = nd => { - const [q, r] = Array.from(quotRem(nd.n, nd.d)); - return Tuple(q, ratio(r, nd.d)); -}; -``` - - ```applescript -- properFracRatio :: Ratio -> (Int, Ratio) on properFracRatio(r) @@ -14,4 +5,13 @@ on properFracRatio(r) set d to d of r Tuple(n div d, ratio(n mod d, d)) end properFracRatio +``` + + +```javascript +// properFracRatio :: Ratio -> (Int, Ratio) +const properFracRatio = nd => { + const [q, r] = Array.from(quotRem(nd.n, nd.d)); + return Tuple(q, ratio(r, nd.d)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/properFraction.md b/MD applescript vs javascript/properFraction.md index 4a4b3ce0..b1f5cbb7 100644 --- a/MD applescript vs javascript/properFraction.md +++ b/MD applescript vs javascript/properFraction.md @@ -1,16 +1,16 @@ -```javascript -// properFraction :: Real -> (Int, Real) -const properFraction = n => { - const i = Math.floor(n) + (n < 0 ? 1 : 0); - return Tuple(i)(n - i); -}; -``` - - ```applescript -- properFraction :: Real -> (Int, Real) on properFraction(n) set i to (n div 1) Tuple(i, n - i) end properFraction +``` + + +```javascript +// properFraction :: Real -> (Int, Real) +const properFraction = n => { + const i = Math.floor(n) + (n < 0 ? 1 : 0); + return Tuple(i)(n - i); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/pureLR.md b/MD applescript vs javascript/pureLR.md index 9800dc70..9dc321ba 100644 --- a/MD applescript vs javascript/pureLR.md +++ b/MD applescript vs javascript/pureLR.md @@ -1,13 +1,13 @@ -```javascript -// pureLR :: a -> Either e a -const pureLR = x => - Right(x); -``` - - ```applescript -- pureLR :: a -> Either e a on pureLR(x) |Right|(x) end pureLR +``` + + +```javascript +// pureLR :: a -> Either e a +const pureLR = x => + Right(x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/pureList.md b/MD applescript vs javascript/pureList.md index a55501eb..63646ccf 100644 --- a/MD applescript vs javascript/pureList.md +++ b/MD applescript vs javascript/pureList.md @@ -1,13 +1,13 @@ -```javascript -// pureList :: a -> [a] -const pureList = x => - [x]; -``` - - ```applescript -- pureList :: a -> [a] on pureList(x) {x} end pure +``` + + +```javascript +// pureList :: a -> [a] +const pureList = x => + [x]; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/pureMay.md b/MD applescript vs javascript/pureMay.md index 1297cb58..fb8c51d8 100644 --- a/MD applescript vs javascript/pureMay.md +++ b/MD applescript vs javascript/pureMay.md @@ -1,13 +1,13 @@ -```javascript -// pureMay :: a -> Maybe a -const pureMay = x => - Just(x); -``` - - ```applescript -- pureMay :: a -> Maybe a on pureMay(x) Just(x) end pureMay +``` + + +```javascript +// pureMay :: a -> Maybe a +const pureMay = x => + Just(x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/pureT.md b/MD applescript vs javascript/pureT.md index 23bba1ac..e2f7ec32 100644 --- a/MD applescript vs javascript/pureT.md +++ b/MD applescript vs javascript/pureT.md @@ -1,23 +1,3 @@ -```javascript -// pureT :: String -> f a -> (a -> f a) -const pureT = t => - // Given a type name string, returns a - // specialised 'pure', where - // 'pure' lifts a value into a particular functor. - x => 'List' !== t ? ( - 'Either' === t ? ( - pureLR(x) - ) : 'Maybe' === t ? ( - pureMay(x) - ) : 'Node' === t ? ( - pureTree(x) - ) : 'Tuple' === t ? ( - pureTuple(x) - ) : pureList(x) - ) : pureList(x); -``` - - ```applescript -- Given a type name string, returns a -- specialised 'pure', where @@ -38,4 +18,24 @@ on pureT(t, x) pureList(x) end if end pureT +``` + + +```javascript +// pureT :: String -> f a -> (a -> f a) +const pureT = t => + // Given a type name string, returns a + // specialised 'pure', where + // 'pure' lifts a value into a particular functor. + x => 'List' !== t ? ( + 'Either' === t ? ( + pureLR(x) + ) : 'Maybe' === t ? ( + pureMay(x) + ) : 'Node' === t ? ( + pureTree(x) + ) : 'Tuple' === t ? ( + pureTuple(x) + ) : pureList(x) + ) : pureList(x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/pureTree.md b/MD applescript vs javascript/pureTree.md index 764fce81..4db59427 100644 --- a/MD applescript vs javascript/pureTree.md +++ b/MD applescript vs javascript/pureTree.md @@ -1,13 +1,13 @@ -```javascript -// pureTree :: a -> Tree a -const pureTree = x => - Node(x)([]); -``` - - ```applescript -- pureTree :: a -> Tree a on pureTree(x) Node(x, {}) end pureTree +``` + + +```javascript +// pureTree :: a -> Tree a +const pureTree = x => + Node(x)([]); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/pureTuple.md b/MD applescript vs javascript/pureTuple.md index 0471d6fd..fc3147dc 100644 --- a/MD applescript vs javascript/pureTuple.md +++ b/MD applescript vs javascript/pureTuple.md @@ -1,13 +1,13 @@ -```javascript -// pureTuple :: a -> (a, a) -const pureTuple = x => - Tuple('')(x); -``` - - ```applescript -- pureTuple :: a -> (a, a) on pureTuple(x) Tuple("", x) end pureTuple +``` + + +```javascript +// pureTuple :: a -> (a, a) +const pureTuple = x => + Tuple('')(x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/quickSort.md b/MD applescript vs javascript/quickSort.md index a0044157..a265b5a6 100644 --- a/MD applescript vs javascript/quickSort.md +++ b/MD applescript vs javascript/quickSort.md @@ -1,21 +1,3 @@ -```javascript -// quickSort :: (Ord a) => [a] -> [a] -const quickSort = xs => - // Included only for comparison with AppleScript - // sort and sortBy are faster and more flexible - xs.length > 1 ? (() => { - const - h = xs[0], - lessMore = partition(x => x <= h)( - xs.slice(1) - ); - return [].concat.apply( - [], [quickSort(lessMore[0]), h, quickSort(lessMore[1])] - ); - })() : xs; -``` - - ```applescript -- Adequate for small sorts, but sort (Ord a => [a] -> [a]), (which uses the ObjC -- sortedArrayUsingSelector) is the one to use @@ -34,4 +16,22 @@ on quickSort(xs) xs end if end quickSort +``` + + +```javascript +// quickSort :: (Ord a) => [a] -> [a] +const quickSort = xs => + // Included only for comparison with AppleScript + // sort and sortBy are faster and more flexible + xs.length > 1 ? (() => { + const + h = xs[0], + lessMore = partition(x => x <= h)( + xs.slice(1) + ); + return [].concat.apply( + [], [quickSort(lessMore[0]), h, quickSort(lessMore[1])] + ); + })() : xs; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/quickSortBy.md b/MD applescript vs javascript/quickSortBy.md index ad8de225..9c3338ac 100644 --- a/MD applescript vs javascript/quickSortBy.md +++ b/MD applescript vs javascript/quickSortBy.md @@ -1,3 +1,23 @@ +```applescript +-- quickSortBy(comparing(my |length|), {"alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", "lambda", "mu"}) +-- quickSortBy :: (a -> a -> Ordering) -> [a] -> [a] +on quickSortBy(cmp, xs) + if length of xs > 1 then + set h to item 1 of xs + script + on |λ|(x) + cmp's |λ|(x, h) ≠ 1 + end |λ| + end script + set {less, more} to partition(result, rest of xs) + quickSortBy(cmp, less) & h & quickSortBy(cmp, more) + else + xs + end if +end quickSortBy +``` + + ```javascript // quickSortBy :: (a -> a -> Ordering) -> [a] -> [a] const quickSortBy = cmp => { @@ -17,24 +37,4 @@ const quickSortBy = cmp => { })() : xs; return go; }; -``` - - -```applescript --- quickSortBy(comparing(my |length|), {"alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", "lambda", "mu"}) --- quickSortBy :: (a -> a -> Ordering) -> [a] -> [a] -on quickSortBy(cmp, xs) - if length of xs > 1 then - set h to item 1 of xs - script - on |λ|(x) - cmp's |λ|(x, h) ≠ 1 - end |λ| - end script - set {less, more} to partition(result, rest of xs) - quickSortBy(cmp, less) & h & quickSortBy(cmp, more) - else - xs - end if -end quickSortBy ``` \ No newline at end of file diff --git a/MD applescript vs javascript/quot.md b/MD applescript vs javascript/quot.md index 2854d0b6..53589672 100644 --- a/MD applescript vs javascript/quot.md +++ b/MD applescript vs javascript/quot.md @@ -1,13 +1,13 @@ -```javascript -// quot :: Int -> Int -> Int -const quot = n => - m => Math.trunc(n / m); -``` - - ```applescript -- quot :: Int -> Int -> Int on quot(m, n) m div n end quot +``` + + +```javascript +// quot :: Int -> Int -> Int +const quot = n => + m => Math.trunc(n / m); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/quotRem.md b/MD applescript vs javascript/quotRem.md index d7b12ad0..5ebb5319 100644 --- a/MD applescript vs javascript/quotRem.md +++ b/MD applescript vs javascript/quotRem.md @@ -1,3 +1,11 @@ +```applescript +-- quotRem :: Int -> Int -> (Int, Int) +on quotRem(m, n) + Tuple(m div n, m mod n) +end quotRem +``` + + ```javascript // quotRem :: Int -> Int -> (Int, Int) const quotRem = m => @@ -5,12 +13,4 @@ const quotRem = m => n => Tuple( Math.trunc(m / n) )(m % n); -``` - - -```applescript --- quotRem :: Int -> Int -> (Int, Int) -on quotRem(m, n) - Tuple(m div n, m mod n) -end quotRem ``` \ No newline at end of file diff --git a/MD applescript vs javascript/quoted.md b/MD applescript vs javascript/quoted.md index 51a3711b..b098405f 100644 --- a/MD applescript vs javascript/quoted.md +++ b/MD applescript vs javascript/quoted.md @@ -1,12 +1,3 @@ -```javascript -// quoted :: Char -> String -> String -const quoted = c => - // A string flanked on both sides - // by a specified quote character. - s => c + s + c; -``` - - ```applescript -- quoted :: Char -> String -> String on quoted(c) @@ -18,4 +9,13 @@ on quoted(c) end |λ| end script end quoted +``` + + +```javascript +// quoted :: Char -> String -> String +const quoted = c => + // A string flanked on both sides + // by a specified quote character. + s => c + s + c; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/radians.md b/MD applescript vs javascript/radians.md index 501156c8..36dbcc81 100644 --- a/MD applescript vs javascript/radians.md +++ b/MD applescript vs javascript/radians.md @@ -1,13 +1,13 @@ -```javascript -// radians :: Float x => Degrees x -> Radians x -const radians = x => - (Math.PI / 180) * x; -``` - - ```applescript -- radians :: Float x => Degrees x -> Radians x on radians(x) (pi / 180) * x end radians +``` + + +```javascript +// radians :: Float x => Degrees x -> Radians x +const radians = x => + (Math.PI / 180) * x; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/raise.md b/MD applescript vs javascript/raise.md index 9d601099..e0b8ca75 100644 --- a/MD applescript vs javascript/raise.md +++ b/MD applescript vs javascript/raise.md @@ -1,14 +1,14 @@ -```javascript -// raise :: Num -> Int -> Num -const raise = x => - // X to the power of n. - n => Math.pow(x, n); -``` - - ```applescript -- raise :: Num -> Int -> Num on raise(m, n) m ^ n end raise +``` + + +```javascript +// raise :: Num -> Int -> Num +const raise = x => + // X to the power of n. + n => Math.pow(x, n); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/randomRInt.md b/MD applescript vs javascript/randomRInt.md index 064e50f7..2f54c39c 100644 --- a/MD applescript vs javascript/randomRInt.md +++ b/MD applescript vs javascript/randomRInt.md @@ -1,16 +1,3 @@ -```javascript -// randomRInt :: Int -> Int -> IO () -> Int -const randomRInt = low => - // The return value of randomRInt is itself - // a function, which, whenever evaluated, - // yields a a new pseudo-random integer - // in the range [low..high]. - high => () => low + Math.floor( - Math.random() * (1 + (high - low)) - ); -``` - - ```applescript -- e.g. map(randomRInt(1, 10), ft(1, 20)) -- randomRInt :: Int -> Int -> IO () -> Int @@ -21,4 +8,17 @@ on randomRInt(low, high) end |λ| end script end randomRInt +``` + + +```javascript +// randomRInt :: Int -> Int -> IO () -> Int +const randomRInt = low => + // The return value of randomRInt is itself + // a function, which, whenever evaluated, + // yields a a new pseudo-random integer + // in the range [low..high]. + high => () => low + Math.floor( + Math.random() * (1 + (high - low)) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/range.md b/MD applescript vs javascript/range.md index 6d340892..b541db74 100644 --- a/MD applescript vs javascript/range.md +++ b/MD applescript vs javascript/range.md @@ -1,3 +1,32 @@ +```applescript +-- range :: Ix a => (a, a) -> [a] +on range(ab) + set {a, b} to {|1| of ab, |2| of ab} + if class of a is list then + set {xs, ys} to {a, b} + else + set {xs, ys} to {{a}, {b}} + end if + set lng to length of xs + + if lng = length of ys then + if lng > 1 then + script + on |λ|(_, i) + enumFromTo(item i of xs, item i of ys) + end |λ| + end script + sequence(map(result, xs)) + else + enumFromTo(a, b) + end if + else + {} + end if +end range +``` + + ```javascript // range :: Ix a => (a, a) -> [a] function range() { @@ -29,33 +58,4 @@ function range() { ) : enumFromTo(as[0])(bs[0]) ) : []; } -``` - - -```applescript --- range :: Ix a => (a, a) -> [a] -on range(ab) - set {a, b} to {|1| of ab, |2| of ab} - if class of a is list then - set {xs, ys} to {a, b} - else - set {xs, ys} to {{a}, {b}} - end if - set lng to length of xs - - if lng = length of ys then - if lng > 1 then - script - on |λ|(_, i) - enumFromTo(item i of xs, item i of ys) - end |λ| - end script - sequence(map(result, xs)) - else - enumFromTo(a, b) - end if - else - {} - end if -end range ``` \ No newline at end of file diff --git a/MD applescript vs javascript/ratio.md b/MD applescript vs javascript/ratio.md index 09f1bd94..f8b348ff 100644 --- a/MD applescript vs javascript/ratio.md +++ b/MD applescript vs javascript/ratio.md @@ -1,20 +1,3 @@ -```javascript -// ratio :: Int -> Int -> Ratio Int -const ratio = x => y => { - const go = (x, y) => - 0 !== y ? (() => { - const d = gcd(x)(y); - return { - type: 'Ratio', - 'n': quot(x)(d), // numerator - 'd': quot(y)(d) // denominator - }; - })() : undefined; - return go(x * signum(y), abs(y)); -}; -``` - - ```applescript -- ratio :: Int -> Int -> Ratio Int on ratio(x, y) @@ -34,4 +17,21 @@ on ratio(x, y) end script go's |λ|(x * (signum(y)), abs(y)) end ratio +``` + + +```javascript +// ratio :: Int -> Int -> Ratio Int +const ratio = x => y => { + const go = (x, y) => + 0 !== y ? (() => { + const d = gcd(x)(y); + return { + type: 'Ratio', + 'n': quot(x)(d), // numerator + 'd': quot(y)(d) // denominator + }; + })() : undefined; + return go(x * signum(y), abs(y)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/ratioDiv.md b/MD applescript vs javascript/ratioDiv.md index a6747874..652310e3 100644 --- a/MD applescript vs javascript/ratioDiv.md +++ b/MD applescript vs javascript/ratioDiv.md @@ -1,3 +1,13 @@ +```applescript +-- ratioDiv :: Rational -> Rational -> Rational +on ratioDiv(n1, n2) + set r1 to rational(n1) + set r2 to rational(n2) + ratio((n of r1) * (d of r2), (d of r1) * (n of r2)) +end ratioDiv +``` + + ```javascript // ratioDiv :: Rational -> Rational -> Rational const ratioDiv = n1 => n2 => { @@ -8,14 +18,4 @@ const ratioDiv = n1 => n2 => { r1.d * r2.n ); }; -``` - - -```applescript --- ratioDiv :: Rational -> Rational -> Rational -on ratioDiv(n1, n2) - set r1 to rational(n1) - set r2 to rational(n2) - ratio((n of r1) * (d of r2), (d of r1) * (n of r2)) -end ratioDiv ``` \ No newline at end of file diff --git a/MD applescript vs javascript/ratioMinus.md b/MD applescript vs javascript/ratioMinus.md index 603a8b4d..12728754 100644 --- a/MD applescript vs javascript/ratioMinus.md +++ b/MD applescript vs javascript/ratioMinus.md @@ -1,15 +1,3 @@ -```javascript -// ratioMinus :: Rational -> Rational -> Rational -const ratioMinus = n1 => n2 => { - const [r1, r2] = [n1, n2].map(rational); - const d = lcm(r1.d)(r2.d); - return ratio((r1.n * (d / r1.d)) - (r2.n * (d / r2.d)))( - d - ); -}; -``` - - ```applescript -- ratioMinus :: Rational -> Rational -> Rational on ratioMinus(n1, n2) @@ -19,4 +7,16 @@ on ratioMinus(n1, n2) ratio((n of r1) * (d / (d of r1) - ¬ ((n of r2) * (d / (d of r2)))), d) end ratioMinus +``` + + +```javascript +// ratioMinus :: Rational -> Rational -> Rational +const ratioMinus = n1 => n2 => { + const [r1, r2] = [n1, n2].map(rational); + const d = lcm(r1.d)(r2.d); + return ratio((r1.n * (d / r1.d)) - (r2.n * (d / r2.d)))( + d + ); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/ratioMult.md b/MD applescript vs javascript/ratioMult.md index fe0c7109..1112786c 100644 --- a/MD applescript vs javascript/ratioMult.md +++ b/MD applescript vs javascript/ratioMult.md @@ -1,3 +1,13 @@ +```applescript +-- ratioMult :: Rational -> Rational -> Rational +on ratioMult(n1, n2) + set r1 to rational(n1) + set r2 to rational(n2) + ratio((n of r1) * (n of r2), (d of r1) * (d of r2)) +end ratioMult +``` + + ```javascript // ratioMult :: Rational -> Rational -> Rational const ratioMult = n1 => n2 => { @@ -8,14 +18,4 @@ const ratioMult = n1 => n2 => { r1.d * r2.d ); }; -``` - - -```applescript --- ratioMult :: Rational -> Rational -> Rational -on ratioMult(n1, n2) - set r1 to rational(n1) - set r2 to rational(n2) - ratio((n of r1) * (n of r2), (d of r1) * (d of r2)) -end ratioMult ``` \ No newline at end of file diff --git a/MD applescript vs javascript/ratioPlus.md b/MD applescript vs javascript/ratioPlus.md index 50006f53..1d576260 100644 --- a/MD applescript vs javascript/ratioPlus.md +++ b/MD applescript vs javascript/ratioPlus.md @@ -1,3 +1,15 @@ +```applescript +-- ratioPlus :: Rational -> Rational -> Rational +on ratioPlus(n1, n2) + set r1 to rational(n1) + set r2 to rational(n2) + set d to lcm(d of r1, d of r2) + ratio((n of r1) * (d / (d of r1) + ¬ + ((n of r2) * (d / (d of r2)))), d) +end ratioPlus +``` + + ```javascript // ratioPlus :: Rational -> Rational -> Rational const ratioPlus = n1 => @@ -8,16 +20,4 @@ const ratioPlus = n1 => d ); }; -``` - - -```applescript --- ratioPlus :: Rational -> Rational -> Rational -on ratioPlus(n1, n2) - set r1 to rational(n1) - set r2 to rational(n2) - set d to lcm(d of r1, d of r2) - ratio((n of r1) * (d / (d of r1) + ¬ - ((n of r2) * (d / (d of r2)))), d) -end ratioPlus ``` \ No newline at end of file diff --git a/MD applescript vs javascript/rational.md b/MD applescript vs javascript/rational.md index dac381ba..d06f9e03 100644 --- a/MD applescript vs javascript/rational.md +++ b/MD applescript vs javascript/rational.md @@ -1,12 +1,12 @@ +```applescript +-- rational :: Num a => a -> Rational on rational(x) set c to class of x if integer is c then ratio(x, 1) else if real is c then approxRatio(missing value, x) else x end if end rational +``` + + ```javascript // rational :: Num a => a -> Rational const rational = x => isNaN(x) ? x : Number.isInteger(x) ? ( ratio(x)(1) ) : approxRatio(undefined)(x); -``` - - -```applescript --- rational :: Num a => a -> Rational on rational(x) set c to class of x if integer is c then ratio(x, 1) else if real is c then approxRatio(missing value, x) else x end if end rational ``` \ No newline at end of file diff --git a/MD applescript vs javascript/read.md b/MD applescript vs javascript/read.md index c23f4d3a..118ad2e8 100644 --- a/MD applescript vs javascript/read.md +++ b/MD applescript vs javascript/read.md @@ -1,12 +1,12 @@ -```javascript -// read :: Read a => String -> a -const read = JSON.parse; -``` - - ```applescript -- read :: Read a => String -> a on read (s) run script s end read +``` + + +```javascript +// read :: Read a => String -> a +const read = JSON.parse; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/readFile.md b/MD applescript vs javascript/readFile.md index 59a41661..a1fdcfc5 100644 --- a/MD applescript vs javascript/readFile.md +++ b/MD applescript vs javascript/readFile.md @@ -1,3 +1,22 @@ +```applescript +-- readFile :: FilePath -> IO String +on readFile(strPath) + set ca to current application + set e to reference + set {s, e} to (ca's NSString's ¬ + stringWithContentsOfFile:((ca's NSString's ¬ + stringWithString:strPath)'s ¬ + stringByStandardizingPath) ¬ + encoding:(ca's NSUTF8StringEncoding) |error|:(e)) + if missing value is e then + s as string + else + (localizedDescription of e) as string + end if +end readFile +``` + + ```javascript // readFile :: FilePath -> IO String const readFile = fp => { @@ -17,23 +36,4 @@ const readFile = fp => { ) : ns ); }; -``` - - -```applescript --- readFile :: FilePath -> IO String -on readFile(strPath) - set ca to current application - set e to reference - set {s, e} to (ca's NSString's ¬ - stringWithContentsOfFile:((ca's NSString's ¬ - stringWithString:strPath)'s ¬ - stringByStandardizingPath) ¬ - encoding:(ca's NSUTF8StringEncoding) |error|:(e)) - if missing value is e then - s as string - else - (localizedDescription of e) as string - end if -end readFile ``` \ No newline at end of file diff --git a/MD applescript vs javascript/readFileLR.md b/MD applescript vs javascript/readFileLR.md index d137decf..194df5ab 100644 --- a/MD applescript vs javascript/readFileLR.md +++ b/MD applescript vs javascript/readFileLR.md @@ -1,21 +1,3 @@ -```javascript -// readFileLR :: FilePath -> Either String IO String -const readFileLR = fp => { - const - e = $(), - ns = $.NSString - .stringWithContentsOfFileEncodingError( - $(fp).stringByStandardizingPath, - $.NSUTF8StringEncoding, - e - ); - return ns.isNil() ? ( - Left(ObjC.unwrap(e.localizedDescription)) - ) : Right(ObjC.unwrap(ns)); -}; -``` - - ```applescript -- readFileLR :: FilePath -> Either String IO String on readFileLR(strPath) @@ -32,4 +14,22 @@ on readFileLR(strPath) |Right|(s as string) end if end readFileLR +``` + + +```javascript +// readFileLR :: FilePath -> Either String IO String +const readFileLR = fp => { + const + e = $(), + ns = $.NSString + .stringWithContentsOfFileEncodingError( + $(fp).stringByStandardizingPath, + $.NSUTF8StringEncoding, + e + ); + return ns.isNil() ? ( + Left(ObjC.unwrap(e.localizedDescription)) + ) : Right(ObjC.unwrap(ns)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/readHex.md b/MD applescript vs javascript/readHex.md index 07e82212..c14f3817 100644 --- a/MD applescript vs javascript/readHex.md +++ b/MD applescript vs javascript/readHex.md @@ -1,11 +1,3 @@ -```javascript -// readHex :: String -> Int -const readHex = s => - // Integer value of hexadecimal expression. - parseInt(s, 16); -``` - - ```applescript -- readHex :: String -> Int on readHex(s) @@ -20,4 +12,12 @@ on readHex(s) end script item 1 of foldr(go, {0, 1}, characters of s) end readHex +``` + + +```javascript +// readHex :: String -> Int +const readHex = s => + // Integer value of hexadecimal expression. + parseInt(s, 16); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/readLR.md b/MD applescript vs javascript/readLR.md index 8b9a5ea7..5cfc3c2d 100644 --- a/MD applescript vs javascript/readLR.md +++ b/MD applescript vs javascript/readLR.md @@ -1,15 +1,3 @@ -```javascript -// readLR :: Read a => String -> Either String a -const readLR = s => { - try { - return Right(JSON.parse(s)); - } catch (e) { - return Left(e.message); - } -}; -``` - - ```applescript -- readLR :: Read a => String -> Either String a on readLR(s) @@ -19,4 +7,16 @@ on readLR(s) |Left|(e) end try end readLR +``` + + +```javascript +// readLR :: Read a => String -> Either String a +const readLR = s => { + try { + return Right(JSON.parse(s)); + } catch (e) { + return Left(e.message); + } +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/recip.md b/MD applescript vs javascript/recip.md index 500bb12e..d5359725 100644 --- a/MD applescript vs javascript/recip.md +++ b/MD applescript vs javascript/recip.md @@ -1,10 +1,3 @@ -```javascript -// recip :: Num -> Num -const recip = n => - 0 !== n ? (1 / n) : undefined; -``` - - ```applescript -- recip :: Num -> Num on recip(n) @@ -14,4 +7,11 @@ on recip(n) missing value end if end recip +``` + + +```javascript +// recip :: Num -> Num +const recip = n => + 0 !== n ? (1 / n) : undefined; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/recipMay.md b/MD applescript vs javascript/recipMay.md index 17a43267..d5274829 100644 --- a/MD applescript vs javascript/recipMay.md +++ b/MD applescript vs javascript/recipMay.md @@ -1,12 +1,3 @@ -```javascript -// recipMay :: Num -> Maybe Num -const recipMay = n => - 0 === n ? ( - Nothing() - ) : Just(1 / n); -``` - - ```applescript -- recipMay :: Num -> Maybe Num on recipMay(n) @@ -16,4 +7,13 @@ on recipMay(n) Nothing() end if end recipMay +``` + + +```javascript +// recipMay :: Num -> Maybe Num +const recipMay = n => + 0 === n ? ( + Nothing() + ) : Just(1 / n); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/regexMatches.md b/MD applescript vs javascript/regexMatches.md index 4e7c6ff5..8b1762d3 100644 --- a/MD applescript vs javascript/regexMatches.md +++ b/MD applescript vs javascript/regexMatches.md @@ -1,3 +1,8 @@ +```applescript +-- regexMatches :: String -> String -> [[String]] on regexMatches(strRegex, strHay) set ca to current application -- NSNotFound handling and and High Sierra workaround due to @sl1974 set NSNotFound to a reference to 9.22337203685477E+18 + 5807 set oRgx to ca's NSRegularExpression's regularExpressionWithPattern:strRegex ¬ options:((ca's NSRegularExpressionAnchorsMatchLines as integer)) ¬ |error|:(missing value) set oString to ca's NSString's stringWithString:strHay script matchString on |λ|(m) script rangeMatched on |λ|(i) tell (m's rangeAtIndex:i) set intFrom to its location if NSNotFound ≠ intFrom then text (intFrom + 1) thru (intFrom + (its |length|)) of strHay else missing value end if end tell end |λ| end script end |λ| end script script asRange on |λ|(x) range() of x end |λ| end script map(asRange, (oRgx's matchesInString:oString ¬ options:0 range:{location:0, |length|:oString's |length|()}) as list) end regexMatches +``` + + ```javascript // regexMatches :: Regex -> String -> [[String]] const regexMatches = rgx => @@ -13,9 +18,4 @@ const regexMatches = rgx => ) : Nothing() )(r.exec(s)); }; -``` - - -```applescript --- regexMatches :: String -> String -> [[String]] on regexMatches(strRegex, strHay) set ca to current application -- NSNotFound handling and and High Sierra workaround due to @sl1974 set NSNotFound to a reference to 9.22337203685477E+18 + 5807 set oRgx to ca's NSRegularExpression's regularExpressionWithPattern:strRegex ¬ options:((ca's NSRegularExpressionAnchorsMatchLines as integer)) ¬ |error|:(missing value) set oString to ca's NSString's stringWithString:strHay script matchString on |λ|(m) script rangeMatched on |λ|(i) tell (m's rangeAtIndex:i) set intFrom to its location if NSNotFound ≠ intFrom then text (intFrom + 1) thru (intFrom + (its |length|)) of strHay else missing value end if end tell end |λ| end script end |λ| end script script asRange on |λ|(x) range() of x end |λ| end script map(asRange, (oRgx's matchesInString:oString ¬ options:0 range:{location:0, |length|:oString's |length|()}) as list) end regexMatches ``` \ No newline at end of file diff --git a/MD applescript vs javascript/rem.md b/MD applescript vs javascript/rem.md index 7e17dcb2..9efec063 100644 --- a/MD applescript vs javascript/rem.md +++ b/MD applescript vs javascript/rem.md @@ -1,12 +1,12 @@ -```javascript -// rem :: Int -> Int -> Int -const rem = n => m => n % m; -``` - - ```applescript -- rem :: Int -> Int -> Int on rem(m, n) m mod n end rem +``` + + +```javascript +// rem :: Int -> Int -> Int +const rem = n => m => n % m; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/removeFile.md b/MD applescript vs javascript/removeFile.md index 04ecf00b..67741e26 100644 --- a/MD applescript vs javascript/removeFile.md +++ b/MD applescript vs javascript/removeFile.md @@ -1,15 +1,3 @@ -```javascript -// removeFile :: FilePath -> Either String String -const removeFile = fp => { - const error = $(); - return $.NSFileManager.defaultManager - .removeItemAtPathError(fp, error) ? ( - Right('Removed: ' + fp) - ) : Left(ObjC.unwrap(error.localizedDescription)); -}; -``` - - ```applescript -- removeFile :: FilePath -> Either String String on removeFile(fp) @@ -22,4 +10,16 @@ on removeFile(fp) |Left|(obj's localizedDescription as string) end if end removeFile +``` + + +```javascript +// removeFile :: FilePath -> Either String String +const removeFile = fp => { + const error = $(); + return $.NSFileManager.defaultManager + .removeItemAtPathError(fp, error) ? ( + Right('Removed: ' + fp) + ) : Left(ObjC.unwrap(error.localizedDescription)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/repeat.md b/MD applescript vs javascript/repeat.md index 628e45a1..c3e8dd96 100644 --- a/MD applescript vs javascript/repeat.md +++ b/MD applescript vs javascript/repeat.md @@ -1,11 +1,3 @@ -```javascript -// repeat :: a -> Generator [a] -function* repeat(x) { - while(true) yield x; -} -``` - - ```applescript -- repeat :: a -> Generator [a] on |repeat|(x) @@ -15,4 +7,12 @@ on |repeat|(x) end |λ| end script end |repeat| +``` + + +```javascript +// repeat :: a -> Generator [a] +function* repeat(x) { + while(true) yield x; +} ``` \ No newline at end of file diff --git a/MD applescript vs javascript/replace.md b/MD applescript vs javascript/replace.md index 2ad76329..6fe64e8a 100644 --- a/MD applescript vs javascript/replace.md +++ b/MD applescript vs javascript/replace.md @@ -1,16 +1,3 @@ -```javascript -// replace :: String -> String -> String -> String -// replace :: Regex -> String -> String -> String -const replace = needle => strNew => strHaystack => - strHaystack.replace( - 'string' !== typeof needle ? ( - needle - ) : new RegExp(needle, 'g'), - strNew - ); -``` - - ```applescript -- replace :: String -> String -> String -> String on replace(strNeedle, strNew, strHayStack) @@ -21,4 +8,17 @@ on replace(strNeedle, strNew, strHayStack) set my text item delimiters to dlm return strReplaced end replace +``` + + +```javascript +// replace :: String -> String -> String -> String +// replace :: Regex -> String -> String -> String +const replace = needle => strNew => strHaystack => + strHaystack.replace( + 'string' !== typeof needle ? ( + needle + ) : new RegExp(needle, 'g'), + strNew + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/replicate.md b/MD applescript vs javascript/replicate.md index 72b799cf..f454c016 100644 --- a/MD applescript vs javascript/replicate.md +++ b/MD applescript vs javascript/replicate.md @@ -1,13 +1,3 @@ -```javascript -// replicate :: Int -> a -> [a] -const replicate = n => - // A list of n copies of x. - x => Array.from({ - length: n - }, () => x); -``` - - ```applescript -- Egyptian multiplication - progressively doubling a list, appending -- stages of doubling to an accumulator where needed for binary @@ -34,4 +24,14 @@ on replicate(n, s) set xs to |until|(p, f, {n, s, ""}) item 2 of xs & item 3 of xs end replicate +``` + + +```javascript +// replicate :: Int -> a -> [a] +const replicate = n => + // A list of n copies of x. + x => Array.from({ + length: n + }, () => x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/replicateM.md b/MD applescript vs javascript/replicateM.md index e49bc460..984b1ea9 100644 --- a/MD applescript vs javascript/replicateM.md +++ b/MD applescript vs javascript/replicateM.md @@ -1,18 +1,3 @@ -```javascript -// replicateM :: Int -> [a] -> [[a]] -const replicateM = n => - // Instance for lists (arrays) only here. - xs => { - const go = x => 0 >= x ? [ - [] - ] : liftA2List(cons)( - list(xs) - )(go(x - 1)); - return go(n); - }; -``` - - ```applescript -- Instance for lists only here @@ -38,4 +23,19 @@ on replicateM(n, xs) go's |λ|(n) end replicateM +``` + + +```javascript +// replicateM :: Int -> [a] -> [[a]] +const replicateM = n => + // Instance for lists (arrays) only here. + xs => { + const go = x => 0 >= x ? [ + [] + ] : liftA2List(cons)( + list(xs) + )(go(x - 1)); + return go(n); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/replicateString.md b/MD applescript vs javascript/replicateString.md index 21af133c..4ba4caaa 100644 --- a/MD applescript vs javascript/replicateString.md +++ b/MD applescript vs javascript/replicateString.md @@ -1,10 +1,3 @@ -```javascript -// replicateString :: Int -> String -> String -const replicateString = n => - s => s.repeat(n); -``` - - ```applescript -- replicateString :: Int -> String -> String on replicateString(n, s) @@ -19,4 +12,11 @@ on replicateString(n, s) end repeat return out & dbl end replicateS +``` + + +```javascript +// replicateString :: Int -> String -> String +const replicateString = n => + s => s.repeat(n); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/reverse.md b/MD applescript vs javascript/reverse.md index e6d4d3cd..fafd0fd2 100644 --- a/MD applescript vs javascript/reverse.md +++ b/MD applescript vs javascript/reverse.md @@ -1,12 +1,3 @@ -```javascript -// reverse :: [a] -> [a] -const reverse = xs => - 'string' !== typeof xs ? ( - xs.slice(0).reverse() - ) : xs.split('').reverse().join(''); -``` - - ```applescript -- reverse :: [a] -> [a] on |reverse|(xs) @@ -16,4 +7,13 @@ on |reverse|(xs) reverse of xs end if end |reverse| +``` + + +```javascript +// reverse :: [a] -> [a] +const reverse = xs => + 'string' !== typeof xs ? ( + xs.slice(0).reverse() + ) : xs.split('').reverse().join(''); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/rights.md b/MD applescript vs javascript/rights.md index 7fe28646..0b2de537 100644 --- a/MD applescript vs javascript/rights.md +++ b/MD applescript vs javascript/rights.md @@ -1,14 +1,3 @@ -```javascript -// rights :: [Either a b] -> [b] -const rights = xs => - xs.flatMap( - x => ('Either' === x.type) && ( - undefined !== x.Right - ) ? [x.Right] : [] - ); -``` - - ```applescript -- rights :: [Either a b] -> [b] on rights(xs) @@ -28,4 +17,15 @@ on rights(xs) end script concatMap(result, xs) end rights +``` + + +```javascript +// rights :: [Either a b] -> [b] +const rights = xs => + xs.flatMap( + x => ('Either' === x.type) && ( + undefined !== x.Right + ) ? [x.Right] : [] + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/root.md b/MD applescript vs javascript/root.md index bd285905..69b767a5 100644 --- a/MD applescript vs javascript/root.md +++ b/MD applescript vs javascript/root.md @@ -1,12 +1,12 @@ -```javascript -// root :: Tree a -> a -const root = tree => tree.root; -``` - - ```applescript -- root :: Tree a -> a on root(oTree) root of oTree end root +``` + + +```javascript +// root :: Tree a -> a +const root = tree => tree.root; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/rotate.md b/MD applescript vs javascript/rotate.md index 222ed6e5..adec5f05 100644 --- a/MD applescript vs javascript/rotate.md +++ b/MD applescript vs javascript/rotate.md @@ -1,3 +1,16 @@ +```applescript +-- rotate :: Int -> [a] -> [a] +on rotate(n, xs) + set lng to |length|(xs) + if missing value is not lng then + take(lng, drop(lng - n, cycle(xs))) + else + lng + end if +end rotate +``` + + ```javascript // rotate :: Int -> [a] -> [a] const rotate = n => xs => { @@ -11,17 +24,4 @@ const rotate = n => xs => { ) ) : undefined; }; -``` - - -```applescript --- rotate :: Int -> [a] -> [a] -on rotate(n, xs) - set lng to |length|(xs) - if missing value is not lng then - take(lng, drop(lng - n, cycle(xs))) - else - lng - end if -end rotate ``` \ No newline at end of file diff --git a/MD applescript vs javascript/round.md b/MD applescript vs javascript/round.md index b831f50a..7ccaf397 100644 --- a/MD applescript vs javascript/round.md +++ b/MD applescript vs javascript/round.md @@ -1,3 +1,11 @@ +```applescript +-- round :: a -> Int +on |round|(n) + round n +end |round| +``` + + ```javascript // round :: a -> Int const round = x => { @@ -12,12 +20,4 @@ const round = x => { ) ); }; -``` - - -```applescript --- round :: a -> Int -on |round|(n) - round n -end |round| ``` \ No newline at end of file diff --git a/MD applescript vs javascript/roundTo.md b/MD applescript vs javascript/roundTo.md index 6c8f0420..d677905b 100644 --- a/MD applescript vs javascript/roundTo.md +++ b/MD applescript vs javascript/roundTo.md @@ -1,13 +1,13 @@ +```applescript +-- Float x rounded to n decimals +-- roundTo :: Int -> Float -> Float on roundTo(n, x) set d to 10 ^ n (round (x * d)) / d end roundTo +``` + + ```javascript // roundTo :: Int -> Float -> Float const roundTo = n => x => { const d = Math.pow(10, n); return Math.round(x * d) / d; }; -``` - - -```applescript --- Float x rounded to n decimals --- roundTo :: Int -> Float -> Float on roundTo(n, x) set d to 10 ^ n (round (x * d)) / d end roundTo ``` \ No newline at end of file diff --git a/MD applescript vs javascript/runAction.md b/MD applescript vs javascript/runAction.md index bca5703d..eee2756f 100644 --- a/MD applescript vs javascript/runAction.md +++ b/MD applescript vs javascript/runAction.md @@ -1,15 +1,15 @@ -```javascript -// runAction :: Action a -> a -const runAction = act => - // Evaluation of an action. - act.act(act.arg); -``` - - ```applescript -- runAction :: Action a -> a on runAction(act) -- Evaluation of an action. tell act to |λ|(its arg) of my mReturn(its act) end runAction +``` + + +```javascript +// runAction :: Action a -> a +const runAction = act => + // Evaluation of an action. + act.act(act.arg); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/safeMay.md b/MD applescript vs javascript/safeMay.md index b46518cb..8d830bc4 100644 --- a/MD applescript vs javascript/safeMay.md +++ b/MD applescript vs javascript/safeMay.md @@ -1,10 +1,3 @@ -```javascript -// safeMay :: (a -> Bool) -> (a -> b) -> Maybe b -const safeMay = p => f => x => - p(x) ? Just(f(x)) : Nothing(); -``` - - ```applescript -- safeMay :: (a -> Bool) -> (a -> b) -> Maybe b on safeMay(p, f, x) @@ -14,4 +7,11 @@ on safeMay(p, f, x) Nothing() end if end safeMay +``` + + +```javascript +// safeMay :: (a -> Bool) -> (a -> b) -> Maybe b +const safeMay = p => f => x => + p(x) ? Just(f(x)) : Nothing(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/scanl.md b/MD applescript vs javascript/scanl.md index 1b9fe3e4..43d36c03 100644 --- a/MD applescript vs javascript/scanl.md +++ b/MD applescript vs javascript/scanl.md @@ -1,13 +1,3 @@ -```javascript -// scanl :: (b -> a -> b) -> b -> [a] -> [b] -const scanl = f => startValue => xs => - list(xs).reduce((a, x) => { - const v = f(a[0])(x); - return Tuple(v)(a[1].concat(v)); - }, Tuple(startValue)([startValue]))[1]; -``` - - ```applescript -- scanl :: (b -> a -> b) -> b -> [a] -> [b] on scanl(f, startValue, xs) @@ -22,4 +12,14 @@ on scanl(f, startValue, xs) return lst end tell end scanl +``` + + +```javascript +// scanl :: (b -> a -> b) -> b -> [a] -> [b] +const scanl = f => startValue => xs => + list(xs).reduce((a, x) => { + const v = f(a[0])(x); + return Tuple(v)(a[1].concat(v)); + }, Tuple(startValue)([startValue]))[1]; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/scanl1.md b/MD applescript vs javascript/scanl1.md index ee32e27d..bdb3ee55 100644 --- a/MD applescript vs javascript/scanl1.md +++ b/MD applescript vs javascript/scanl1.md @@ -1,3 +1,15 @@ +```applescript +-- scanl1 :: (a -> a -> a) -> [a] -> [a] +on scanl1(f, xs) + if 0 < length of xs then + scanl(f, item 1 of xs, rest of xs) + else + {} + end if +end scanl +``` + + ```javascript // scanl1 :: (a -> a -> a) -> [a] -> [a] const scanl1 = f => @@ -8,16 +20,4 @@ const scanl1 = f => xs[0] )(xs.slice(1)) ) : []; -``` - - -```applescript --- scanl1 :: (a -> a -> a) -> [a] -> [a] -on scanl1(f, xs) - if 0 < length of xs then - scanl(f, item 1 of xs, rest of xs) - else - {} - end if -end scanl ``` \ No newline at end of file diff --git a/MD applescript vs javascript/scanr.md b/MD applescript vs javascript/scanr.md index 92b10ebc..c705983f 100644 --- a/MD applescript vs javascript/scanr.md +++ b/MD applescript vs javascript/scanr.md @@ -1,13 +1,3 @@ -```javascript -// scanr :: (b -> a -> b) -> b -> [a] -> [b] -const scanr = f => - startValue => xs => list(xs).reduceRight((a, x) => { - const v = f(x)(a[0]); - return Tuple(v)([v].concat(a[1])); - }, Tuple(startValue)([startValue]))[1]; -``` - - ```applescript -- scanr :: (b -> a -> b) -> b -> [a] -> [b] on scanr(f, startValue, xs) @@ -22,4 +12,14 @@ on scanr(f, startValue, xs) return reverse of lst end tell end scanr +``` + + +```javascript +// scanr :: (b -> a -> b) -> b -> [a] -> [b] +const scanr = f => + startValue => xs => list(xs).reduceRight((a, x) => { + const v = f(x)(a[0]); + return Tuple(v)([v].concat(a[1])); + }, Tuple(startValue)([startValue]))[1]; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/scanr1.md b/MD applescript vs javascript/scanr1.md index 05fac5fc..3070ebf4 100644 --- a/MD applescript vs javascript/scanr1.md +++ b/MD applescript vs javascript/scanr1.md @@ -1,3 +1,15 @@ +```applescript +-- scanr1 :: (a -> a -> a) -> [a] -> [a] +on scanr1(f, xs) + if length of xs > 0 then + scanr(f, item -1 of xs, init(xs)) + else + {} + end if +end scanr1 +``` + + ```javascript // scanr1 :: (a -> a -> a) -> [a] -> [a] const scanr1 = f => @@ -9,16 +21,4 @@ const scanr1 = f => xs.slice(-1)[0] )(xs.slice(0, -1)) ) : []; -``` - - -```applescript --- scanr1 :: (a -> a -> a) -> [a] -> [a] -on scanr1(f, xs) - if length of xs > 0 then - scanr(f, item -1 of xs, init(xs)) - else - {} - end if -end scanr1 ``` \ No newline at end of file diff --git a/MD applescript vs javascript/second.md b/MD applescript vs javascript/second.md index 4fe42588..936b2e65 100644 --- a/MD applescript vs javascript/second.md +++ b/MD applescript vs javascript/second.md @@ -1,15 +1,3 @@ -```javascript -// second :: (a -> b) -> ((c, a) -> (c, b)) -const second = f => - // A function over a simple value lifted - // to a function over a tuple. - // f (a, b) -> (a, f(b)) - xy => Tuple(xy[0])( - f(xy[1]) - ); -``` - - ```applescript -- second :: (a -> b) -> ((c, a) -> (c, b)) on |second|(f) @@ -21,4 +9,16 @@ on |second|(f) end |λ| end script end |second| +``` + + +```javascript +// second :: (a -> b) -> ((c, a) -> (c, b)) +const second = f => + // A function over a simple value lifted + // to a function over a tuple. + // f (a, b) -> (a, f(b)) + xy => Tuple(xy[0])( + f(xy[1]) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/sequenceA.md b/MD applescript vs javascript/sequenceA.md index 4093ceca..47c09e81 100644 --- a/MD applescript vs javascript/sequenceA.md +++ b/MD applescript vs javascript/sequenceA.md @@ -1,12 +1,3 @@ -```javascript -// sequenceA :: (Applicative f, Traversable t) => t (f a) -> f (t a) -const sequenceA = tfa => - traverse(x => x)( - tfa - ); -``` - - ```applescript -- sequenceA :: (Applicative f, Traversable t) => t (f a) -> f (t a) on sequenceA(tfa) @@ -17,4 +8,13 @@ on sequenceA(tfa) end script traverse(identity, tfa) end sequenceA +``` + + +```javascript +// sequenceA :: (Applicative f, Traversable t) => t (f a) -> f (t a) +const sequenceA = tfa => + traverse(x => x)( + tfa + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/setCurrentDirectory.md b/MD applescript vs javascript/setCurrentDirectory.md index d74109c3..6c30d8aa 100644 --- a/MD applescript vs javascript/setCurrentDirectory.md +++ b/MD applescript vs javascript/setCurrentDirectory.md @@ -1,14 +1,3 @@ -```javascript -// setCurrentDirectory :: String -> IO () -const setCurrentDirectory = strPath => - $.NSFileManager.defaultManager - .changeCurrentDirectoryPath( - ObjC.wrap(strPath) - .stringByStandardizingPath - ); -``` - - ```applescript -- setCurrentDirectory :: String -> IO () on setCurrentDirectory(strPath) @@ -20,4 +9,15 @@ on setCurrentDirectory(strPath) changeCurrentDirectoryPath:oPath end if end setCurrentDirectory +``` + + +```javascript +// setCurrentDirectory :: String -> IO () +const setCurrentDirectory = strPath => + $.NSFileManager.defaultManager + .changeCurrentDirectoryPath( + ObjC.wrap(strPath) + .stringByStandardizingPath + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/setFromList.md b/MD applescript vs javascript/setFromList.md index 208d7c26..26d9247b 100644 --- a/MD applescript vs javascript/setFromList.md +++ b/MD applescript vs javascript/setFromList.md @@ -1,10 +1,3 @@ -```javascript -// setFromList :: Ord a => [a] -> Set a -const setFromList = xs => - new Set(xs); -``` - - ```applescript -- setFromList :: Ord a => [a] -> Set a on setFromList(xs) @@ -15,4 +8,11 @@ on setFromList(xs) ca's NSMutableSet's ¬ setWithArray:(ca's NSArray's arrayWithArray:(xs)) end setFromList +``` + + +```javascript +// setFromList :: Ord a => [a] -> Set a +const setFromList = xs => + new Set(xs); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/setInsert.md b/MD applescript vs javascript/setInsert.md index a822f4f9..0ad7a800 100644 --- a/MD applescript vs javascript/setInsert.md +++ b/MD applescript vs javascript/setInsert.md @@ -1,14 +1,14 @@ -```javascript -// setInsert :: Ord a => a -> Set a -> Set a -const setInsert = x => oSet => - oSet.add(x); -``` - - ```applescript -- setInsert :: Ord a => a -> Set a -> Set a on setInsert(x, objcSet) objcSet's addObject:(x) objcSet end setInsert +``` + + +```javascript +// setInsert :: Ord a => a -> Set a -> Set a +const setInsert = x => oSet => + oSet.add(x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/setMember.md b/MD applescript vs javascript/setMember.md index ffff6eb2..24d4ba6e 100644 --- a/MD applescript vs javascript/setMember.md +++ b/MD applescript vs javascript/setMember.md @@ -1,13 +1,13 @@ -```javascript -// setMember :: Ord a => a -> Set a -> Bool -const setMember = x => oSet => - oSet.has(x); -``` - - ```applescript -- setMember :: Ord a => a -> Set a -> Bool on setMember(x, objcSet) missing value is not (objcSet's member:(x)) end setMember +``` + + +```javascript +// setMember :: Ord a => a -> Set a -> Bool +const setMember = x => oSet => + oSet.has(x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/setSize.md b/MD applescript vs javascript/setSize.md index bb2b8773..ee544268 100644 --- a/MD applescript vs javascript/setSize.md +++ b/MD applescript vs javascript/setSize.md @@ -1,13 +1,13 @@ -```javascript -// setSize :: Set a -> Int -const setSize = oSet => - oSet.size; -``` - - ```applescript -- setSize :: Set a -> Int on setSize(objcSet) objcSet's |count|() as integer end setSize +``` + + +```javascript +// setSize :: Set a -> Int +const setSize = oSet => + oSet.size; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/shift.md b/MD applescript vs javascript/shift.md index 0071e459..9244244b 100644 --- a/MD applescript vs javascript/shift.md +++ b/MD applescript vs javascript/shift.md @@ -1,16 +1,3 @@ -```javascript -// shift :: Int -> [a] -> [a] -const shift = n => xs => { - const lng = length(xs); - return Infinity > lng ? ( - take(lng)( - drop(n)(cycle(xs)) - ) - ) : (drop(n)(xs), xs); -}; -``` - - ```applescript -- shift :: Int -> [a] -> [a] on shift(n, xs) @@ -21,4 +8,17 @@ on shift(n, xs) drop(n, xs) end if end shift +``` + + +```javascript +// shift :: Int -> [a] -> [a] +const shift = n => xs => { + const lng = length(xs); + return Infinity > lng ? ( + take(lng)( + drop(n)(cycle(xs)) + ) + ) : (drop(n)(xs), xs); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/show.md b/MD applescript vs javascript/show.md index e948c4b2..3ab876b1 100644 --- a/MD applescript vs javascript/show.md +++ b/MD applescript vs javascript/show.md @@ -1,44 +1,3 @@ -```javascript -// show :: a -> String -// show :: a -> Int -> Indented String -const show = x => { - const - e = ('function' !== typeof x) ? ( - x - ) : { - type: 'Function', - f: x - }; - return JSON.stringify(e, (_, v) => { - const - f = ((null !== v) && (undefined !== v)) ? (() => { - const t = v.type; - return 'Either' === t ? ( - showLR - ) : 'Function' === t ? ( - dct => 'λ' + dct.f.toString() - ) : 'Maybe' === t ? ( - showMaybe - ) : 'Ordering' === t ? ( - showOrdering - ) : 'Ratio' === t ? ( - showRatio - ) : 'string' === typeof t && t.startsWith('Tuple') ? ( - showTuple - ) : Array.isArray(v) ? ( - showList - ) : undefined; - })() : showUndefined; - return Boolean(f) ? ( - f(v) - ) : 'string' !== typeof v ? ( - v - ) : v; - }); -}; -``` - - ```applescript -- show :: a -> String on show(e) @@ -86,4 +45,45 @@ on show(e) end try end if end show +``` + + +```javascript +// show :: a -> String +// show :: a -> Int -> Indented String +const show = x => { + const + e = ('function' !== typeof x) ? ( + x + ) : { + type: 'Function', + f: x + }; + return JSON.stringify(e, (_, v) => { + const + f = ((null !== v) && (undefined !== v)) ? (() => { + const t = v.type; + return 'Either' === t ? ( + showLR + ) : 'Function' === t ? ( + dct => 'λ' + dct.f.toString() + ) : 'Maybe' === t ? ( + showMaybe + ) : 'Ordering' === t ? ( + showOrdering + ) : 'Ratio' === t ? ( + showRatio + ) : 'string' === typeof t && t.startsWith('Tuple') ? ( + showTuple + ) : Array.isArray(v) ? ( + showList + ) : undefined; + })() : showUndefined; + return Boolean(f) ? ( + f(v) + ) : 'string' !== typeof v ? ( + v + ) : v; + }); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showBinary.md b/MD applescript vs javascript/showBinary.md index 598265bc..fd6ac079 100644 --- a/MD applescript vs javascript/showBinary.md +++ b/MD applescript vs javascript/showBinary.md @@ -1,3 +1,16 @@ +```applescript +-- showBinary :: Int -> String +on showBinary(n) + script binaryChar + on |λ|(n) + text item (n + 1) of "01" + end |λ| + end script + showIntAtBase(2, binaryChar, n, "") +end showBin +``` + + ```javascript // showBinary :: Int -> String const showBinary = n => { @@ -9,17 +22,4 @@ const showBinary = n => { binaryChar )(n)(''); }; -``` - - -```applescript --- showBinary :: Int -> String -on showBinary(n) - script binaryChar - on |λ|(n) - text item (n + 1) of "01" - end |λ| - end script - showIntAtBase(2, binaryChar, n, "") -end showBin ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showDate.md b/MD applescript vs javascript/showDate.md index c12a3eb5..a11c9d13 100644 --- a/MD applescript vs javascript/showDate.md +++ b/MD applescript vs javascript/showDate.md @@ -1,13 +1,13 @@ -```javascript -// showDate :: Date -> String -const showDate = JSON.stringify; -``` - - ```applescript -- ISO 8601 UTC -- showDate :: Date -> String on showDate(dte) ((dte - (time to GMT)) as «class isot» as string) & ".000Z" end showDate +``` + + +```javascript +// showDate :: Date -> String +const showDate = JSON.stringify; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showDict.md b/MD applescript vs javascript/showDict.md index 1982ae5a..ba34a7df 100644 --- a/MD applescript vs javascript/showDict.md +++ b/MD applescript vs javascript/showDict.md @@ -1,12 +1,12 @@ -```javascript -// showDict :: Dict -> String -const showDict = show; -``` - - ```applescript -- showDict :: Dict -> String on showDict(dct) showJSON(dct) end showDict +``` + + +```javascript +// showDict :: Dict -> String +const showDict = show; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showForest.md b/MD applescript vs javascript/showForest.md index d0357d1f..eb2114e9 100644 --- a/MD applescript vs javascript/showForest.md +++ b/MD applescript vs javascript/showForest.md @@ -1,3 +1,11 @@ +```applescript +-- showForest :: [Tree a] -> String +on showForest(xs) + unlines(map(my showTree, xs)) +end showForest +``` + + ```javascript // showForest :: [Tree a] -> String const showForest = xs => @@ -6,12 +14,4 @@ const showForest = xs => x ) ))); -``` - - -```applescript --- showForest :: [Tree a] -> String -on showForest(xs) - unlines(map(my showTree, xs)) -end showForest ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showHex.md b/MD applescript vs javascript/showHex.md index 3e5a6134..6f616ce2 100644 --- a/MD applescript vs javascript/showHex.md +++ b/MD applescript vs javascript/showHex.md @@ -1,15 +1,15 @@ +```applescript +-- showHex :: Int -> String +on showHex(n) + showIntAtBase(16, mReturn(intToDigit), n, "") +end showHex +``` + + ```javascript // showHex :: Int -> String const showHex = n => showIntAtBase(16)( intToDigit )(n)(''); -``` - - -```applescript --- showHex :: Int -> String -on showHex(n) - showIntAtBase(16, mReturn(intToDigit), n, "") -end showHex ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showIntAtBase.md b/MD applescript vs javascript/showIntAtBase.md index e29a3548..f2a19cb9 100644 --- a/MD applescript vs javascript/showIntAtBase.md +++ b/MD applescript vs javascript/showIntAtBase.md @@ -1,21 +1,3 @@ -```javascript -// showIntAtBase :: Int -> (Int -> Char) -> Int -> String -> String -const showIntAtBase = base => toChr => n => rs => { - const go = ([n, d], r) => { - const r_ = toChr(d) + r; - return 0 !== n ? ( - go(Array.from(quotRem(n)(base)), r_) - ) : r_; - }; - return 1 >= base ? ( - 'error: showIntAtBase applied to unsupported base' - ) : 0 > n ? ( - 'error: showIntAtBase applied to negative number' - ) : go(Array.from(quotRem(n)(base)), rs); -}; -``` - - ```applescript -- showIntAtBase :: Int -> (Int -> Char) -> Int -> String -> String on showIntAtBase(base, toDigit, n, rs) @@ -33,4 +15,22 @@ on showIntAtBase(base, toDigit, n, rs) end script showIt's |λ|(quotRem(n, base), rs) end showIntAtBase +``` + + +```javascript +// showIntAtBase :: Int -> (Int -> Char) -> Int -> String -> String +const showIntAtBase = base => toChr => n => rs => { + const go = ([n, d], r) => { + const r_ = toChr(d) + r; + return 0 !== n ? ( + go(Array.from(quotRem(n)(base)), r_) + ) : r_; + }; + return 1 >= base ? ( + 'error: showIntAtBase applied to unsupported base' + ) : 0 > n ? ( + 'error: showIntAtBase applied to negative number' + ) : go(Array.from(quotRem(n)(base)), rs); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showJSON.md b/MD applescript vs javascript/showJSON.md index a60fbc98..b3eeb573 100644 --- a/MD applescript vs javascript/showJSON.md +++ b/MD applescript vs javascript/showJSON.md @@ -1,11 +1,3 @@ -```javascript -// showJSON :: a -> String -const showJSON = x => - // Indented JSON representation of the value x. - JSON.stringify(x, null, 2); -``` - - ```applescript -- showJSON :: a -> String on showJSON(x) @@ -34,4 +26,12 @@ on showJSON(x) end try end if end showJSON +``` + + +```javascript +// showJSON :: a -> String +const showJSON = x => + // Indented JSON representation of the value x. + JSON.stringify(x, null, 2); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showLR.md b/MD applescript vs javascript/showLR.md index 46bd5b1f..f86c4d52 100644 --- a/MD applescript vs javascript/showLR.md +++ b/MD applescript vs javascript/showLR.md @@ -1,14 +1,3 @@ -```javascript -// showLR :: Either a b -> String -const showLR = lr => { - const k = undefined !== lr.Left ? ( - 'Left' - ) : 'Right'; - return k + '(' + unQuoted(show(lr[k])) + ')'; -}; -``` - - ```applescript -- showLR :: Either a b -> String on showLR(lr) @@ -18,4 +7,15 @@ on showLR(lr) "Left(" & unQuoted(show(|Left| of lr)) & ")" end if end showLR +``` + + +```javascript +// showLR :: Either a b -> String +const showLR = lr => { + const k = undefined !== lr.Left ? ( + 'Left' + ) : 'Right'; + return k + '(' + unQuoted(show(lr[k])) + ')'; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showList.md b/MD applescript vs javascript/showList.md index c95855fa..3425fac7 100644 --- a/MD applescript vs javascript/showList.md +++ b/MD applescript vs javascript/showList.md @@ -1,15 +1,15 @@ +```applescript +-- showList :: [a] -> String +on showList(xs) + "[" & intercalateS(", ", map(my show, xs)) & "]" +end showList +``` + + ```javascript // showList :: [a] -> String const showList = xs => '[' + xs.map(show) .join(', ') .replace(/[\"]/g, '') + ']'; -``` - - -```applescript --- showList :: [a] -> String -on showList(xs) - "[" & intercalateS(", ", map(my show, xs)) & "]" -end showList ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showLog.md b/MD applescript vs javascript/showLog.md index de79d731..41d7c8ce 100644 --- a/MD applescript vs javascript/showLog.md +++ b/MD applescript vs javascript/showLog.md @@ -1,3 +1,11 @@ +```applescript +-- showLog :: a -> IO () +on showLog(e) + log show(e) +end showLog +``` + + ```javascript // showLog :: a -> IO () const showLog = (...args) => @@ -6,12 +14,4 @@ const showLog = (...args) => .map(JSON.stringify) .join(' -> ') ); -``` - - -```applescript --- showLog :: a -> IO () -on showLog(e) - log show(e) -end showLog ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showMaybe.md b/MD applescript vs javascript/showMaybe.md index 09a582a0..77e5a0b8 100644 --- a/MD applescript vs javascript/showMaybe.md +++ b/MD applescript vs javascript/showMaybe.md @@ -1,12 +1,3 @@ -```javascript -// showMaybe :: Maybe a -> String -const showMaybe = mb => - mb.Nothing ? ( - 'Nothing' - ) : 'Just(' + unQuoted(show(mb.Just)) + ')'; -``` - - ```applescript -- showMaybe :: Maybe a -> String on showMaybe(mb) @@ -16,4 +7,13 @@ on showMaybe(mb) "Just " & unQuoted(show(Just of mb)) end if end showMaybe +``` + + +```javascript +// showMaybe :: Maybe a -> String +const showMaybe = mb => + mb.Nothing ? ( + 'Nothing' + ) : 'Just(' + unQuoted(show(mb.Just)) + ')'; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showOrdering.md b/MD applescript vs javascript/showOrdering.md index a0668f6a..94e72175 100644 --- a/MD applescript vs javascript/showOrdering.md +++ b/MD applescript vs javascript/showOrdering.md @@ -1,14 +1,3 @@ -```javascript -// showOrdering :: Ordering -> String -const showOrdering = e => - 0 < e.value ? ( - 'GT' - ) : 0 > e.value ? ( - 'LT' - ) : 'EQ'; -``` - - ```applescript -- showOrdering :: Ordering -> String on showOrdering(e) @@ -21,4 +10,15 @@ on showOrdering(e) "EQ" end if end showOrdering +``` + + +```javascript +// showOrdering :: Ordering -> String +const showOrdering = e => + 0 < e.value ? ( + 'GT' + ) : 0 > e.value ? ( + 'LT' + ) : 'EQ'; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showOutline.md b/MD applescript vs javascript/showOutline.md index 4fc164ca..7978410a 100644 --- a/MD applescript vs javascript/showOutline.md +++ b/MD applescript vs javascript/showOutline.md @@ -1,17 +1,3 @@ -```javascript -// showOutline :: Tree String -> String -const showOutline = tree => { - const go = indent => x => - unlines( - [indent + x.root].concat( - x.nest.flatMap(go(' ' + indent)) - ) - ); - return go('')(tree); -}; -``` - - ```applescript -- showOutline :: Tree String -> String on showOutline(x) @@ -28,4 +14,18 @@ on showOutline(x) end script unlines((go's |λ|(""))'s |λ|(x)) end showOutline +``` + + +```javascript +// showOutline :: Tree String -> String +const showOutline = tree => { + const go = indent => x => + unlines( + [indent + x.root].concat( + x.nest.flatMap(go(' ' + indent)) + ) + ); + return go('')(tree); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showPrecision.md b/MD applescript vs javascript/showPrecision.md index 696a8a51..51d71a57 100644 --- a/MD applescript vs javascript/showPrecision.md +++ b/MD applescript vs javascript/showPrecision.md @@ -1,3 +1,12 @@ +```applescript +-- showPrecision :: Int -> Float -> String +on showPrecision(n, x) + set d to 10 ^ n + ((round (x * d)) / d) as string +end showPrecision +``` + + ```javascript // showPrecision :: Int -> Float -> String const showPrecision = n => x => { @@ -6,13 +15,4 @@ const showPrecision = n => x => { const d = Math.pow(10, n); return str(Math.round(d * x) / d); }; -``` - - -```applescript --- showPrecision :: Int -> Float -> String -on showPrecision(n, x) - set d to 10 ^ n - ((round (x * d)) / d) as string -end showPrecision ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showRatio.md b/MD applescript vs javascript/showRatio.md index fdb396b9..7079bafe 100644 --- a/MD applescript vs javascript/showRatio.md +++ b/MD applescript vs javascript/showRatio.md @@ -1,16 +1,3 @@ -```javascript -// showRatio :: Ratio -> String -const showRatio = r => - 'Ratio' !== r.type ? ( - r.toString() - ) : r.n.toString() + ( - 1 !== r.d ? ( - '/' + r.d.toString() - ) : '' - ); -``` - - ```applescript -- showRatio :: Ratio -> String on showRatio(r) @@ -22,4 +9,17 @@ on showRatio(r) s end if end showRatio +``` + + +```javascript +// showRatio :: Ratio -> String +const showRatio = r => + 'Ratio' !== r.type ? ( + r.toString() + ) : r.n.toString() + ( + 1 !== r.d ? ( + '/' + r.d.toString() + ) : '' + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showSet.md b/MD applescript vs javascript/showSet.md index bb864680..458247f2 100644 --- a/MD applescript vs javascript/showSet.md +++ b/MD applescript vs javascript/showSet.md @@ -1,12 +1,3 @@ -```javascript -// showSet :: Set a -> String -const showSet = oSet => - '{' + Array.from(oSet) - .map(x => x.toString()) - .join(',') + '}'; -``` - - ```applescript -- showSet :: Set a -> String on showSet(s) @@ -17,4 +8,13 @@ on showSet(s) end script "{" & intercalate(", ", map(str, sort(elems(s)))) & "}" end showSet +``` + + +```javascript +// showSet :: Set a -> String +const showSet = oSet => + '{' + Array.from(oSet) + .map(x => x.toString()) + .join(',') + '}'; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showTree.md b/MD applescript vs javascript/showTree.md index 1e73c5f1..0f086908 100644 --- a/MD applescript vs javascript/showTree.md +++ b/MD applescript vs javascript/showTree.md @@ -1,12 +1,3 @@ -```javascript -// showTree :: Tree a -> String -const showTree = x => - drawTree2(false)(true)( - fmapTree(show)(x) - ); -``` - - ```applescript -- showTree :: Tree a -> String on showTree(tree) @@ -17,4 +8,13 @@ on showTree(tree) end script drawTree2(false, true, fmapTree(str, tree)) end showTree +``` + + +```javascript +// showTree :: Tree a -> String +const showTree = x => + drawTree2(false)(true)( + fmapTree(show)(x) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showTuple.md b/MD applescript vs javascript/showTuple.md index 64ec0b12..539e4681 100644 --- a/MD applescript vs javascript/showTuple.md +++ b/MD applescript vs javascript/showTuple.md @@ -1,12 +1,3 @@ -```javascript -// showTuple :: Tuple -> String -const showTuple = tpl => - '(' + enumFromTo(0)(tpl.length - 1) - .map(x => unQuoted(show(tpl[x]))) - .join(',') + ')'; -``` - - ```applescript -- showTuple :: Tuple -> String on showTuple(tpl) @@ -23,4 +14,13 @@ on showTuple(tpl) end script "(" & intercalateS(", ", map(result, enumFromTo(1, length of tpl))) & ")" end showTuple +``` + + +```javascript +// showTuple :: Tuple -> String +const showTuple = tpl => + '(' + enumFromTo(0)(tpl.length - 1) + .map(x => unQuoted(show(tpl[x]))) + .join(',') + ')'; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/showUndefined.md b/MD applescript vs javascript/showUndefined.md index 5905b8a5..546acace 100644 --- a/MD applescript vs javascript/showUndefined.md +++ b/MD applescript vs javascript/showUndefined.md @@ -1,12 +1,12 @@ -```javascript -// showUndefined :: () -> String -const showUndefined = () => '(⊥)'; -``` - - ```applescript -- showUndefined :: () -> String on showUndefined() "⊥" end showUndefined +``` + + +```javascript +// showUndefined :: () -> String +const showUndefined = () => '(⊥)'; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/signum.md b/MD applescript vs javascript/signum.md index ae962765..c69466b6 100644 --- a/MD applescript vs javascript/signum.md +++ b/MD applescript vs javascript/signum.md @@ -1,3 +1,17 @@ +```applescript +-- signum :: Num -> Num +on signum(x) + if x < 0 then + -1 + else if x = 0 then + 0 + else + 1 + end if +end signum +``` + + ```javascript // signum :: Num -> Num const signum = n => @@ -13,18 +27,4 @@ const signum = n => ) : ( 0 < n ? 1 : 0 ); -``` - - -```applescript --- signum :: Num -> Num -on signum(x) - if x < 0 then - -1 - else if x = 0 then - 0 - else - 1 - end if -end signum ``` \ No newline at end of file diff --git a/MD applescript vs javascript/sj.md b/MD applescript vs javascript/sj.md index 274411b6..0a615ca9 100644 --- a/MD applescript vs javascript/sj.md +++ b/MD applescript vs javascript/sj.md @@ -1,3 +1,12 @@ +```applescript +-- Abbreviation for quick testing +-- sj :: a -> String +on sj(x) + showJSON(x) +end sj +``` + + ```javascript // sj :: a -> String function sj() { @@ -13,13 +22,4 @@ function sj() { ] : [args[0], null, 2] ); } -``` - - -```applescript --- Abbreviation for quick testing --- sj :: a -> String -on sj(x) - showJSON(x) -end sj ``` \ No newline at end of file diff --git a/MD applescript vs javascript/snd.md b/MD applescript vs javascript/snd.md index 07f81da6..ef7f6c9e 100644 --- a/MD applescript vs javascript/snd.md +++ b/MD applescript vs javascript/snd.md @@ -1,11 +1,3 @@ -```javascript -// snd :: (a, b) -> b -const snd = tpl => - // Second member of a pair. - tpl[1]; -``` - - ```applescript -- snd :: (a, b) -> b on snd(tpl) @@ -15,4 +7,12 @@ on snd(tpl) item 2 of tpl end if end snd +``` + + +```javascript +// snd :: (a, b) -> b +const snd = tpl => + // Second member of a pair. + tpl[1]; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/snoc.md b/MD applescript vs javascript/snoc.md index 9d08ac9e..7994db14 100644 --- a/MD applescript vs javascript/snoc.md +++ b/MD applescript vs javascript/snoc.md @@ -1,13 +1,3 @@ -```javascript -// snoc :: [a] -> a -> [a] -const snoc = xs => - // The mirror image of cons - // A new copy of the given list, - // with an atom appended at the end. - x => list(xs).concat(x); -``` - - ```applescript -- Mirror image of cons -- New copy of the list, with an atom added at the end @@ -15,4 +5,14 @@ const snoc = xs => on snoc(xs, x) xs & {x} end snoc +``` + + +```javascript +// snoc :: [a] -> a -> [a] +const snoc = xs => + // The mirror image of cons + // A new copy of the given list, + // with an atom appended at the end. + x => list(xs).concat(x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/sort.md b/MD applescript vs javascript/sort.md index 14a7be26..4022c095 100644 --- a/MD applescript vs javascript/sort.md +++ b/MD applescript vs javascript/sort.md @@ -1,14 +1,14 @@ -```javascript -// sort :: Ord a => [a] -> [a] -const sort = xs => list(xs).slice() - .sort((a, b) => a < b ? -1 : (a > b ? 1 : 0)); -``` - - ```applescript -- sort :: Ord a => [a] -> [a] on sort(xs) ((current application's NSArray's arrayWithArray:xs)'s ¬ sortedArrayUsingSelector:"compare:") as list end sort +``` + + +```javascript +// sort :: Ord a => [a] -> [a] +const sort = xs => list(xs).slice() + .sort((a, b) => a < b ? -1 : (a > b ? 1 : 0)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/sortBy.md b/MD applescript vs javascript/sortBy.md index dca99bae..59b7d1e1 100644 --- a/MD applescript vs javascript/sortBy.md +++ b/MD applescript vs javascript/sortBy.md @@ -1,11 +1,3 @@ -```javascript -// sortBy :: (a -> a -> Ordering) -> [a] -> [a] -const sortBy = f => - xs => list(xs).slice() - .sort((a, b) => f(a)(b)); -``` - - ```applescript -- Enough for small scale sorts. -- Use instead sortOn (Ord b => (a -> b) -> [a] -> [a]) @@ -28,4 +20,12 @@ on sortBy(f, xs) xs end if end sortBy +``` + + +```javascript +// sortBy :: (a -> a -> Ordering) -> [a] -> [a] +const sortBy = f => + xs => list(xs).slice() + .sort((a, b) => f(a)(b)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/sortOn.md b/MD applescript vs javascript/sortOn.md index 441ad619..9fd94108 100644 --- a/MD applescript vs javascript/sortOn.md +++ b/MD applescript vs javascript/sortOn.md @@ -1,17 +1,3 @@ -```javascript -// sortOn :: Ord b => (a -> b) -> [a] -> [a] -const sortOn = f => - // Equivalent to sortBy(comparing(f)), but with f(x) - // evaluated only once for each x in xs. - // ('Schwartzian' decorate-sort-undecorate). - xs => xs.map( - x => Tuple(f(x))(x) - ) - .sort(uncurry(comparing(fst))) - .map(snd); -``` - - ```applescript -- Sort a list by comparing the results of a key function applied to each -- element. sortOn f is equivalent to sortBy(comparing(f), xs), but has the @@ -87,4 +73,18 @@ on sortOn(f, xs) map(undec, ((ca's NSArray's arrayWithArray:map(dec, xs))'s ¬ sortedArrayUsingDescriptors:map(descrip, bs)) as list) end sortOn +``` + + +```javascript +// sortOn :: Ord b => (a -> b) -> [a] -> [a] +const sortOn = f => + // Equivalent to sortBy(comparing(f)), but with f(x) + // evaluated only once for each x in xs. + // ('Schwartzian' decorate-sort-undecorate). + xs => xs.map( + x => Tuple(f(x))(x) + ) + .sort(uncurry(comparing(fst))) + .map(snd); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/span.md b/MD applescript vs javascript/span.md index cea89eeb..bbccb78f 100644 --- a/MD applescript vs javascript/span.md +++ b/MD applescript vs javascript/span.md @@ -1,23 +1,3 @@ -```javascript -// span :: (a -> Bool) -> [a] -> ([a], [a]) -const span = p => - // Longest prefix of xs consisting of elements which - // all satisfy p, tupled with the remainder of xs. - xs => { - const - ys = 'string' !== typeof xs ? ( - list(xs) - ) : xs, - iLast = ys.length - 1; - return splitAt( - until( - i => iLast < i || !p(ys[i]) - )(i => 1 + i)(0) - )(ys); - }; -``` - - ```applescript -- span :: (a -> Bool) -> [a] -> ([a], [a]) on span(f) @@ -38,4 +18,24 @@ on span(f) end |λ| end script end span +``` + + +```javascript +// span :: (a -> Bool) -> [a] -> ([a], [a]) +const span = p => + // Longest prefix of xs consisting of elements which + // all satisfy p, tupled with the remainder of xs. + xs => { + const + ys = 'string' !== typeof xs ? ( + list(xs) + ) : xs, + iLast = ys.length - 1; + return splitAt( + until( + i => iLast < i || !p(ys[i]) + )(i => 1 + i)(0) + )(ys); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/splitArrow (***).md b/MD applescript vs javascript/splitArrow (***).md index 5dcf79d7..f1285a3f 100644 --- a/MD applescript vs javascript/splitArrow (***).md +++ b/MD applescript vs javascript/splitArrow (***).md @@ -1,15 +1,3 @@ -```javascript -// splitArrow (***) :: (a -> b) -> (c -> d) -> ((a, c) -> (b, d)) -const splitArrow = f => - // The functions f and g combined in a single function - // from a tuple (x, y) to a tuple of (f(x), g(y)) - // (see bimap) - g => tpl => Tuple(f(tpl[0]))( - g(tpl[1]) - ); -``` - - ```applescript -- Compose a function (from a tuple to a tuple), -- (with separate transformations for fst and snd) @@ -21,4 +9,16 @@ on splitArrow(f, g) end |λ| end script end splitArrow +``` + + +```javascript +// splitArrow (***) :: (a -> b) -> (c -> d) -> ((a, c) -> (b, d)) +const splitArrow = f => + // The functions f and g combined in a single function + // from a tuple (x, y) to a tuple of (f(x), g(y)) + // (see bimap) + g => tpl => Tuple(f(tpl[0]))( + g(tpl[1]) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/splitAt.md b/MD applescript vs javascript/splitAt.md index 10383cb5..680e07da 100644 --- a/MD applescript vs javascript/splitAt.md +++ b/MD applescript vs javascript/splitAt.md @@ -1,12 +1,3 @@ -```javascript -// splitAt :: Int -> [a] -> ([a], [a]) -const splitAt = n => - xs => Tuple(xs.slice(0, n))( - xs.slice(n) - ); -``` - - ```applescript -- splitAt :: Int -> [a] -> ([a], [a]) on splitAt(n, xs) @@ -25,4 +16,13 @@ on splitAt(n, xs) end if end if end splitAt +``` + + +```javascript +// splitAt :: Int -> [a] -> ([a], [a]) +const splitAt = n => + xs => Tuple(xs.slice(0, n))( + xs.slice(n) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/splitBy.md b/MD applescript vs javascript/splitBy.md index 201e5a4a..f5ac8286 100644 --- a/MD applescript vs javascript/splitBy.md +++ b/MD applescript vs javascript/splitBy.md @@ -1,30 +1,3 @@ -```javascript -// splitBy :: (a -> a -> Bool) -> [a] -> [[a]] -// splitBy :: (String -> String -> Bool) -> String -> [String] -const splitBy = p => - // Splitting not on a delimiter, but wherever the relationship - // between consecutive terms matches a binary predicate. - xs => (xs.length < 2) ? [xs] : (() => { - const - bln = 'string' === typeof xs, - ys = bln ? xs.split('') : xs, - h = ys[0], - parts = ys.slice(1) - .reduce(([acc, active, prev], x) => - p(prev)(x) ? ( - [acc.concat([active]), [x], x] - ) : [acc, active.concat(x), x], [ - [], - [h], - h - ]); - return (bln ? ( - ps => ps.map(cs => ''.concat.apply('', cs)) - ) : x => x)(parts[0].concat([parts[1]])); - })(); -``` - - ```applescript -- splitBy :: (a -> a -> Bool) -> [a] -> [[a]] -- splitBy :: (String -> String -> Bool) -> String -> [String] @@ -60,4 +33,31 @@ on splitBy(p, xs) end if end if end splitBy +``` + + +```javascript +// splitBy :: (a -> a -> Bool) -> [a] -> [[a]] +// splitBy :: (String -> String -> Bool) -> String -> [String] +const splitBy = p => + // Splitting not on a delimiter, but wherever the relationship + // between consecutive terms matches a binary predicate. + xs => (xs.length < 2) ? [xs] : (() => { + const + bln = 'string' === typeof xs, + ys = bln ? xs.split('') : xs, + h = ys[0], + parts = ys.slice(1) + .reduce(([acc, active, prev], x) => + p(prev)(x) ? ( + [acc.concat([active]), [x], x] + ) : [acc, active.concat(x), x], [ + [], + [h], + h + ]); + return (bln ? ( + ps => ps.map(cs => ''.concat.apply('', cs)) + ) : x => x)(parts[0].concat([parts[1]])); + })(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/splitFileName.md b/MD applescript vs javascript/splitFileName.md index e8bb8ca6..a5ae3f2c 100644 --- a/MD applescript vs javascript/splitFileName.md +++ b/MD applescript vs javascript/splitFileName.md @@ -1,21 +1,3 @@ -```javascript -// splitFileName :: FilePath -> (String, String) -const splitFileName = strPath => - // Tuple of directory and file name, derived from file path. - // Inverse of combine. - ('' !== strPath) ? ( - ('/' !== strPath[strPath.length - 1]) ? (() => { - const - xs = strPath.split('/'), - stem = xs.slice(0, -1); - return stem.length > 0 ? ( - Tuple(stem.join('/') + '/')(xs.slice(-1)[0]) - ) : Tuple('./')(xs.slice(-1)[0]); - })() : Tuple(strPath)('') - ) : Tuple('./')(''); -``` - - ```applescript -- Split a filename into directory and file. combine is the inverse. -- splitFileName :: FilePath -> (String, String) @@ -36,4 +18,22 @@ on splitFileName(strPath) Tuple("./", "") end if end splitFileName +``` + + +```javascript +// splitFileName :: FilePath -> (String, String) +const splitFileName = strPath => + // Tuple of directory and file name, derived from file path. + // Inverse of combine. + ('' !== strPath) ? ( + ('/' !== strPath[strPath.length - 1]) ? (() => { + const + xs = strPath.split('/'), + stem = xs.slice(0, -1); + return stem.length > 0 ? ( + Tuple(stem.join('/') + '/')(xs.slice(-1)[0]) + ) : Tuple('./')(xs.slice(-1)[0]); + })() : Tuple(strPath)('') + ) : Tuple('./')(''); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/splitOn.md b/MD applescript vs javascript/splitOn.md index c4b33229..45b020bf 100644 --- a/MD applescript vs javascript/splitOn.md +++ b/MD applescript vs javascript/splitOn.md @@ -1,25 +1,3 @@ -```javascript -// splitOn :: [a] -> [a] -> [[a]] -// splitOn :: String -> String -> [String] -const splitOn = pat => src => - /* A list of the strings delimited by - instances of a given pattern in s. */ - ('string' === typeof src) ? ( - src.split(pat) - ) : (() => { - const - lng = pat.length, - tpl = findIndices(matching(pat))(src).reduce( - (a, i) => Tuple( - fst(a).concat([src.slice(snd(a), i)]) - )(lng + i), - Tuple([])(0) - ); - return fst(tpl).concat([src.slice(snd(tpl))]); - })(); -``` - - ```applescript -- splitOn("\r\n", "a\r\nb\r\nd\r\ne") --> ["a","b","d","e"] -- splitOn("aaa", "aaaXaaaXaaaXaaa") --> {"", "X", "X", "X", ""} @@ -48,4 +26,26 @@ on splitOn(pat, src) return fst(tpl) & {drop(snd(tpl) - 1, src)} end if end splitOn +``` + + +```javascript +// splitOn :: [a] -> [a] -> [[a]] +// splitOn :: String -> String -> [String] +const splitOn = pat => src => + /* A list of the strings delimited by + instances of a given pattern in s. */ + ('string' === typeof src) ? ( + src.split(pat) + ) : (() => { + const + lng = pat.length, + tpl = findIndices(matching(pat))(src).reduce( + (a, i) => Tuple( + fst(a).concat([src.slice(snd(a), i)]) + )(lng + i), + Tuple([])(0) + ); + return fst(tpl).concat([src.slice(snd(tpl))]); + })(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/splitRegex.md b/MD applescript vs javascript/splitRegex.md index 489f80d0..7fb41850 100644 --- a/MD applescript vs javascript/splitRegex.md +++ b/MD applescript vs javascript/splitRegex.md @@ -1,10 +1,3 @@ -```javascript -// splitRegex :: Regex -> String -> [String] -const splitRegex = needle => haystack => - haystack.split(needle); -``` - - ```applescript -- splitRegex :: Regex -> String -> [String] on splitRegex(strRegex, str) @@ -36,4 +29,11 @@ on splitRegex(strRegex, str) {str} end if end splitRegex +``` + + +```javascript +// splitRegex :: Regex -> String -> [String] +const splitRegex = needle => haystack => + haystack.split(needle); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/sqrt.md b/MD applescript vs javascript/sqrt.md index 4538299d..be719fa3 100644 --- a/MD applescript vs javascript/sqrt.md +++ b/MD applescript vs javascript/sqrt.md @@ -1,10 +1,3 @@ -```javascript -// sqrt :: Num -> Num -const sqrt = n => - (0 <= n) ? Math.sqrt(n) : undefined; -``` - - ```applescript -- sqrt :: Num -> Num on sqrt(n) @@ -14,4 +7,11 @@ on sqrt(n) missing value end if end sqrt +``` + + +```javascript +// sqrt :: Num -> Num +const sqrt = n => + (0 <= n) ? Math.sqrt(n) : undefined; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/sqrtLR.md b/MD applescript vs javascript/sqrtLR.md index 1394f0b6..cd84a14a 100644 --- a/MD applescript vs javascript/sqrtLR.md +++ b/MD applescript vs javascript/sqrtLR.md @@ -1,12 +1,3 @@ -```javascript -// sqrtLR :: Num -> Either String Num -const sqrtLR = n => - 0 > n ? ( - Left('Square root of negative number: ' + n) - ) : Right(Math.sqrt(n)); -``` - - ```applescript -- sqrtLR :: Num -> Either String Num on sqrtLR(n) @@ -16,4 +7,13 @@ on sqrtLR(n) |Left|("Square root of negative number: " & n) end if end sqrtLR +``` + + +```javascript +// sqrtLR :: Num -> Either String Num +const sqrtLR = n => + 0 > n ? ( + Left('Square root of negative number: ' + n) + ) : Right(Math.sqrt(n)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/sqrtMay.md b/MD applescript vs javascript/sqrtMay.md index 5bd7cc63..d4973b70 100644 --- a/MD applescript vs javascript/sqrtMay.md +++ b/MD applescript vs javascript/sqrtMay.md @@ -1,12 +1,3 @@ -```javascript -// sqrtMay :: Num -> Maybe Num -const sqrtMay = n => - 0 > n ? ( - Nothing() - ) : Just(Math.sqrt(n)); -``` - - ```applescript -- sqrtMay :: Num -> Maybe Num on sqrtMay(n) @@ -16,4 +7,13 @@ on sqrtMay(n) Nothing() end if end sqrtMay +``` + + +```javascript +// sqrtMay :: Num -> Maybe Num +const sqrtMay = n => + 0 > n ? ( + Nothing() + ) : Just(Math.sqrt(n)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/str.md b/MD applescript vs javascript/str.md index 7a62149e..a2ca01f1 100644 --- a/MD applescript vs javascript/str.md +++ b/MD applescript vs javascript/str.md @@ -1,3 +1,11 @@ +```applescript +-- str :: a -> String +on str(x) + x as string +end str +``` + + ```javascript // str :: a -> String const str = x => @@ -6,12 +14,4 @@ const str = x => ) ? ( x.join('') ) : x.toString(); -``` - - -```applescript --- str :: a -> String -on str(x) - x as string -end str ``` \ No newline at end of file diff --git a/MD applescript vs javascript/strip.md b/MD applescript vs javascript/strip.md index 1da35ccb..9f8e342d 100644 --- a/MD applescript vs javascript/strip.md +++ b/MD applescript vs javascript/strip.md @@ -1,10 +1,3 @@ -```javascript -// strip :: String -> String -const strip = s => - s.trim(); -``` - - ```applescript -- strip :: String -> String on strip(s) @@ -16,4 +9,11 @@ on strip(s) end script dropWhile(isSpace, dropWhileEnd(isSpace, s)) end strip +``` + + +```javascript +// strip :: String -> String +const strip = s => + s.trim(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/stripEnd.md b/MD applescript vs javascript/stripEnd.md index 707c36ea..1256d118 100644 --- a/MD applescript vs javascript/stripEnd.md +++ b/MD applescript vs javascript/stripEnd.md @@ -1,13 +1,13 @@ -```javascript -// stripEnd :: String -> String -const stripEnd = s => - s.trimEnd(); -``` - - ```applescript -- stripEnd :: String -> String on stripEnd(s) dropWhileEnd(my isSpace, s) end stripEnd +``` + + +```javascript +// stripEnd :: String -> String +const stripEnd = s => + s.trimEnd(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/stripPrefix.md b/MD applescript vs javascript/stripPrefix.md index f7e5e218..ad297d22 100644 --- a/MD applescript vs javascript/stripPrefix.md +++ b/MD applescript vs javascript/stripPrefix.md @@ -1,23 +1,3 @@ -```javascript -// stripPrefix :: Eq a => [a] -> [a] -> Maybe [a] -const stripPrefix = pfx => - s => { - const - blnString = 'string' === typeof pfx, - [xs, ys] = blnString ? ( - [pfx.split(''), s.split('')] - ) : [pfx, s]; - const - sp_ = (xs, ys) => 0 === xs.length ? ( - Just(blnString ? ys.join('') : ys) - ) : (0 === ys.length || xs[0] !== ys[0]) ? ( - Nothing() - ) : sp_(xs.slice(1), ys.slice(1)); - return sp_(xs, ys); - }; -``` - - ```applescript -- stripPrefix :: Eq a => [a] -> [a] -> Maybe [a] -- stripPrefix :: String -> String -> Maybe String @@ -49,4 +29,24 @@ on stripPrefix(pfx, s) end script |λ|(xs, ys) of result end stripPrefix +``` + + +```javascript +// stripPrefix :: Eq a => [a] -> [a] -> Maybe [a] +const stripPrefix = pfx => + s => { + const + blnString = 'string' === typeof pfx, + [xs, ys] = blnString ? ( + [pfx.split(''), s.split('')] + ) : [pfx, s]; + const + sp_ = (xs, ys) => 0 === xs.length ? ( + Just(blnString ? ys.join('') : ys) + ) : (0 === ys.length || xs[0] !== ys[0]) ? ( + Nothing() + ) : sp_(xs.slice(1), ys.slice(1)); + return sp_(xs, ys); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/stripStart.md b/MD applescript vs javascript/stripStart.md index 858d4d9b..0911b851 100644 --- a/MD applescript vs javascript/stripStart.md +++ b/MD applescript vs javascript/stripStart.md @@ -1,13 +1,13 @@ -```javascript -// stripStart :: String -> String -const stripStart = s => - s.trimStart(); -``` - - ```applescript -- stripStart :: String -> String on stripStart(s) dropWhile(my isSpace, s) end stripStart +``` + + +```javascript +// stripStart :: String -> String +const stripStart = s => + s.trimStart(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/subTreeAtPath.md b/MD applescript vs javascript/subTreeAtPath.md index 916b7e6e..7bd920a4 100644 --- a/MD applescript vs javascript/subTreeAtPath.md +++ b/MD applescript vs javascript/subTreeAtPath.md @@ -1,20 +1,3 @@ -```javascript -// subTreeAtPath :: Tree String -> [String] -> Maybe Tree String -const subTreeAtPath = tree => path => { - const go = (nest, xs) => - 0 < nest.length && 0 < xs.length ? (() => { - const h = xs[0]; - return bindMay(find(t => h === t.root, nest))( - t => 1 < xs.length ? ( - go(t.nest, xs.slice(1)) - ) : Just(t) - ); - })() : Nothing(); - return go([tree], path); -}; -``` - - ```applescript -- subTreeAtPath :: Tree String -> [String] -> Maybe Tree String on subTreeAtPath(tree, pathSegments) @@ -44,4 +27,21 @@ on subTreeAtPath(tree, pathSegments) end script |λ|({tree}, pathSegments) of go end subTreeAtPath +``` + + +```javascript +// subTreeAtPath :: Tree String -> [String] -> Maybe Tree String +const subTreeAtPath = tree => path => { + const go = (nest, xs) => + 0 < nest.length && 0 < xs.length ? (() => { + const h = xs[0]; + return bindMay(find(t => h === t.root, nest))( + t => 1 < xs.length ? ( + go(t.nest, xs.slice(1)) + ) : Just(t) + ); + })() : Nothing(); + return go([tree], path); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/subsequences.md b/MD applescript vs javascript/subsequences.md index 3fd17e9f..0c2b289f 100644 --- a/MD applescript vs javascript/subsequences.md +++ b/MD applescript vs javascript/subsequences.md @@ -1,26 +1,3 @@ -```javascript -// subsequences :: [a] -> [[a]] -// subsequences :: String -> [String] -const subsequences = xs => { - // subsequences([1,2,3]) -> [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] - // subsequences('abc') -> ["","a","b","ab","c","ac","bc","abc"] - const - // nonEmptySubsequences :: [a] -> [[a]] - nonEmptySubsequences = xxs => { - if (xxs.length < 1) return []; - const [x, xs] = [xxs[0], xxs.slice(1)]; - const f = (r, ys) => cons(ys)(cons(cons(x)(ys))(r)); - return cons([x])(nonEmptySubsequences(xs) - .reduceRight(f, [])); - }; - return ('string' === typeof xs) ? ( - cons('')(nonEmptySubsequences(xs.split('')) - .map(x => ''.concat.apply('', x))) // map(concat) - ) : cons([])(nonEmptySubsequences(xs)); -}; -``` - - ```applescript -- subsequences([1,2,3]) -> [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] -- subsequences("abc") -> ["","a","b","ab","c","ac","bc","abc"] @@ -50,4 +27,27 @@ on subsequences(xs) cons([], |λ|(xs) of nonEmptySubsequences) end if end subsequences +``` + + +```javascript +// subsequences :: [a] -> [[a]] +// subsequences :: String -> [String] +const subsequences = xs => { + // subsequences([1,2,3]) -> [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] + // subsequences('abc') -> ["","a","b","ab","c","ac","bc","abc"] + const + // nonEmptySubsequences :: [a] -> [[a]] + nonEmptySubsequences = xxs => { + if (xxs.length < 1) return []; + const [x, xs] = [xxs[0], xxs.slice(1)]; + const f = (r, ys) => cons(ys)(cons(cons(x)(ys))(r)); + return cons([x])(nonEmptySubsequences(xs) + .reduceRight(f, [])); + }; + return ('string' === typeof xs) ? ( + cons('')(nonEmptySubsequences(xs.split('')) + .map(x => ''.concat.apply('', x))) // map(concat) + ) : cons([])(nonEmptySubsequences(xs)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/subsets.md b/MD applescript vs javascript/subsets.md index cad3187f..6ab68e54 100644 --- a/MD applescript vs javascript/subsets.md +++ b/MD applescript vs javascript/subsets.md @@ -1,22 +1,3 @@ -```javascript -// subsets :: [a] -> [[a]] -const subsets = xs => { - const go = ys => - 0 < ys.length ? (() => { - const - h = ys[0], - zs = go(ys.slice(1)); - return zs.concat( - zs.map(z => [h].concat(z)) - ); - })() : [ - [] - ]; - return go(xs); -}; -``` - - ```applescript -- subsets :: [a] -> [[a]] on subsets(xs) @@ -38,4 +19,23 @@ on subsets(xs) end script go's |λ|(xs) end subsets +``` + + +```javascript +// subsets :: [a] -> [[a]] +const subsets = xs => { + const go = ys => + 0 < ys.length ? (() => { + const + h = ys[0], + zs = go(ys.slice(1)); + return zs.concat( + zs.map(z => [h].concat(z)) + ); + })() : [ + [] + ]; + return go(xs); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/subtract.md b/MD applescript vs javascript/subtract.md index b6011eb4..ebb40c3a 100644 --- a/MD applescript vs javascript/subtract.md +++ b/MD applescript vs javascript/subtract.md @@ -1,13 +1,13 @@ -```javascript -// subtract :: Num -> Num -> Num -const subtract = x => - y => y - x; -``` - - ```applescript -- subtract :: Num -> Num -> Num on subtract(x, y) y - x end subtract +``` + + +```javascript +// subtract :: Num -> Num -> Num +const subtract = x => + y => y - x; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/succ.md b/MD applescript vs javascript/succ.md index 114ea500..b352fdfb 100644 --- a/MD applescript vs javascript/succ.md +++ b/MD applescript vs javascript/succ.md @@ -1,3 +1,15 @@ +```applescript +-- succ :: Enum a => a -> a +on succ(x) + if isChar(x) then + chr(1 + ord(x)) + else + 1 + x + end if +end succ +``` + + ```javascript // succ :: Enum a => a -> a const succ = x => { @@ -11,16 +23,4 @@ const succ = x => { 1 + x ) : Error('succ :: Num out of range.'); }; -``` - - -```applescript --- succ :: Enum a => a -> a -on succ(x) - if isChar(x) then - chr(1 + ord(x)) - else - 1 + x - end if -end succ ``` \ No newline at end of file diff --git a/MD applescript vs javascript/succMay.md b/MD applescript vs javascript/succMay.md index 5c7b0b4a..16fff6f2 100644 --- a/MD applescript vs javascript/succMay.md +++ b/MD applescript vs javascript/succMay.md @@ -1,3 +1,15 @@ +```applescript +-- succMay :: Enum a => a -> Maybe a +on succMay(x) + if x is maxBound(x) then + Nothing() + else + Just(toEnum(x)'s |λ|(fromEnum(x) + 1)) + end if +end succMay +``` + + ```javascript // succMay :: Enum a => a -> Maybe a const succMay = x => { @@ -11,16 +23,4 @@ const succMay = x => { Just(1 + x) ) : Nothing(); }; -``` - - -```applescript --- succMay :: Enum a => a -> Maybe a -on succMay(x) - if x is maxBound(x) then - Nothing() - else - Just(toEnum(x)'s |λ|(fromEnum(x) + 1)) - end if -end succMay ``` \ No newline at end of file diff --git a/MD applescript vs javascript/sum.md b/MD applescript vs javascript/sum.md index a885ddbd..f8d2ddc6 100644 --- a/MD applescript vs javascript/sum.md +++ b/MD applescript vs javascript/sum.md @@ -1,11 +1,3 @@ -```javascript -// sum :: [Num] -> Num -const sum = xs => - // The numeric sum of all values in xs. - xs.reduce((a, x) => a + x, 0); -``` - - ```applescript -- sum :: [Num] -> Num on sum(xs) @@ -17,4 +9,12 @@ on sum(xs) foldl(add, 0, xs) end sum +``` + + +```javascript +// sum :: [Num] -> Num +const sum = xs => + // The numeric sum of all values in xs. + xs.reduce((a, x) => a + x, 0); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/swap.md b/MD applescript vs javascript/swap.md index d9289755..dc4356ce 100644 --- a/MD applescript vs javascript/swap.md +++ b/MD applescript vs javascript/swap.md @@ -1,13 +1,3 @@ -```javascript -// swap :: (a, b) -> (b, a) -const swap = ab => - // The pair ab with its order reversed. - Tuple(ab[1])( - ab[0] - ); -``` - - ```applescript -- swap :: (a, b) -> (b, a) on swap(ab) @@ -17,4 +7,14 @@ on swap(ab) {item 2 of ab, item 1 of ab} end if end swap +``` + + +```javascript +// swap :: (a, b) -> (b, a) +const swap = ab => + // The pair ab with its order reversed. + Tuple(ab[1])( + ab[0] + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/tail.md b/MD applescript vs javascript/tail.md index 0ac3a06a..3e6050fe 100644 --- a/MD applescript vs javascript/tail.md +++ b/MD applescript vs javascript/tail.md @@ -1,17 +1,3 @@ -```javascript -// tail :: [a] -> [a] -const tail = xs => - // A new list consisting of all - // items of xs except the first. - 'GeneratorFunction' !== xs.constructor - .constructor.name ? ( - (ys => 0 < ys.length ? ys.slice(1) : [])( - list(xs) - ) - ) : (take(1)(xs), xs); -``` - - ```applescript -- tail :: [a] -> [a] on tail(xs) @@ -34,4 +20,18 @@ on tail(xs) end if end if end tail +``` + + +```javascript +// tail :: [a] -> [a] +const tail = xs => + // A new list consisting of all + // items of xs except the first. + 'GeneratorFunction' !== xs.constructor + .constructor.name ? ( + (ys => 0 < ys.length ? ys.slice(1) : [])( + list(xs) + ) + ) : (take(1)(xs), xs); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/tailMay.md b/MD applescript vs javascript/tailMay.md index eda14027..8bfa45bc 100644 --- a/MD applescript vs javascript/tailMay.md +++ b/MD applescript vs javascript/tailMay.md @@ -1,13 +1,3 @@ -```javascript -// tailMay :: [a] -> Maybe [a] -const tailMay = xs => ( - ys => 0 < ys.length ? ( - Just(ys.slice(1)) - ) : Nothing() -)(list(xs)); -``` - - ```applescript -- tailMay :: [a] -> Maybe [a] on tailMay(xs) @@ -17,4 +7,14 @@ on tailMay(xs) Just(rest of xs) end if end tailMay +``` + + +```javascript +// tailMay :: [a] -> Maybe [a] +const tailMay = xs => ( + ys => 0 < ys.length ? ( + Just(ys.slice(1)) + ) : Nothing() +)(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/tails.md b/MD applescript vs javascript/tails.md index 5ad103d7..7a8f0253 100644 --- a/MD applescript vs javascript/tails.md +++ b/MD applescript vs javascript/tails.md @@ -1,14 +1,3 @@ -```javascript -// tails :: [a] -> [[a]] -const tails = xs => ( - es => es.map((_, i) => es.slice(i)) - .concat([ - [] - ]) -)(list(xs)); -``` - - ```applescript -- tails :: [a] -> [[a]] on tails(xs) @@ -24,4 +13,15 @@ on tails(xs) end script map(residue, es) & {{}} end tails +``` + + +```javascript +// tails :: [a] -> [[a]] +const tails = xs => ( + es => es.map((_, i) => es.slice(i)) + .concat([ + [] + ]) +)(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/take.md b/MD applescript vs javascript/take.md index 671723ff..e7f9e05b 100644 --- a/MD applescript vs javascript/take.md +++ b/MD applescript vs javascript/take.md @@ -1,21 +1,3 @@ -```javascript -// take :: Int -> [a] -> [a] -// take :: Int -> String -> String -const take = n => - // The first n elements of a list, - // string of characters, or stream. - xs => 'GeneratorFunction' !== xs - .constructor.constructor.name ? ( - xs.slice(0, n) - ) : [].concat.apply([], Array.from({ - length: n - }, () => { - const x = xs.next(); - return x.done ? [] : [x.value]; - })); -``` - - ```applescript -- take :: Int -> [a] -> [a] -- take :: Int -> String -> String @@ -48,4 +30,22 @@ on take(n, xs) missing value end if end take +``` + + +```javascript +// take :: Int -> [a] -> [a] +// take :: Int -> String -> String +const take = n => + // The first n elements of a list, + // string of characters, or stream. + xs => 'GeneratorFunction' !== xs + .constructor.constructor.name ? ( + xs.slice(0, n) + ) : [].concat.apply([], Array.from({ + length: n + }, () => { + const x = xs.next(); + return x.done ? [] : [x.value]; + })); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/takeAround.md b/MD applescript vs javascript/takeAround.md index 5bfd0a1c..721530f4 100644 --- a/MD applescript vs javascript/takeAround.md +++ b/MD applescript vs javascript/takeAround.md @@ -1,14 +1,3 @@ -```javascript -// takeAround :: (a -> Bool) -> [a] -> [a] -const takeAround = p => xs => { - const ys = takeWhile(p)(xs); - return ys.length < xs.length ? ( - ys.concat(takeWhileR(p)(xs)) - ) : ys; -}; -``` - - ```applescript -- takeAround :: (a -> Bool) -> [a] -> [a] on takeAround(p, xs) @@ -19,4 +8,15 @@ on takeAround(p, xs) ys end if end takeAround +``` + + +```javascript +// takeAround :: (a -> Bool) -> [a] -> [a] +const takeAround = p => xs => { + const ys = takeWhile(p)(xs); + return ys.length < xs.length ? ( + ys.concat(takeWhileR(p)(xs)) + ) : ys; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/takeBaseName.md b/MD applescript vs javascript/takeBaseName.md index bf2e984e..98a4326b 100644 --- a/MD applescript vs javascript/takeBaseName.md +++ b/MD applescript vs javascript/takeBaseName.md @@ -1,17 +1,3 @@ -```javascript -// takeBaseName :: FilePath -> String -const takeBaseName = strPath => - ('' !== strPath) ? ( - ('/' !== strPath[strPath.length - 1]) ? (() => { - const fn = strPath.split('/').slice(-1)[0]; - return fn.includes('.') ? ( - fn.split('.').slice(0, -1).join('.') - ) : fn; - })() : '' - ) : ''; -``` - - ```applescript -- takeBaseName :: FilePath -> String on takeBaseName(strPath) @@ -30,4 +16,18 @@ on takeBaseName(strPath) "" end if end takeBaseName +``` + + +```javascript +// takeBaseName :: FilePath -> String +const takeBaseName = strPath => + ('' !== strPath) ? ( + ('/' !== strPath[strPath.length - 1]) ? (() => { + const fn = strPath.split('/').slice(-1)[0]; + return fn.includes('.') ? ( + fn.split('.').slice(0, -1).join('.') + ) : fn; + })() : '' + ) : ''; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/takeCycle.md b/MD applescript vs javascript/takeCycle.md index cb8699c4..ef8d14ea 100644 --- a/MD applescript vs javascript/takeCycle.md +++ b/MD applescript vs javascript/takeCycle.md @@ -1,22 +1,3 @@ -```javascript -// takeCycle :: Int -> [a] -> [a] -const takeCycle = n => - // First n elements of a non-finite cycle of xs. - xs => { - const lng = xs.length; - return ( - n <= xs ? ( - xs - ) : concat( - replicate(Math.ceil(n / lng))( - xs - ) - ) - ).slice(0, n); - }; -``` - - ```applescript -- takeCycle :: Int -> [a] -> [a] on takeCycle(n, xs) @@ -33,4 +14,23 @@ on takeCycle(n, xs) items 1 thru n of cycle end if end takeCycle +``` + + +```javascript +// takeCycle :: Int -> [a] -> [a] +const takeCycle = n => + // First n elements of a non-finite cycle of xs. + xs => { + const lng = xs.length; + return ( + n <= xs ? ( + xs + ) : concat( + replicate(Math.ceil(n / lng))( + xs + ) + ) + ).slice(0, n); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/takeDirectory.md b/MD applescript vs javascript/takeDirectory.md index 956a931e..6f112d58 100644 --- a/MD applescript vs javascript/takeDirectory.md +++ b/MD applescript vs javascript/takeDirectory.md @@ -1,14 +1,3 @@ -```javascript -// takeDirectory :: FilePath -> FilePath -const takeDirectory = fp => - '' !== fp ? ( - (xs => xs.length > 0 ? xs.join('/') : '.')( - fp.split('/').slice(0, -1) - ) - ) : '.'; -``` - - ```applescript -- takeDirectory :: FilePath -> FilePath on takeDirectory(fp) @@ -28,4 +17,15 @@ on takeDirectory(fp) "." end if end takeDirectory +``` + + +```javascript +// takeDirectory :: FilePath -> FilePath +const takeDirectory = fp => + '' !== fp ? ( + (xs => xs.length > 0 ? xs.join('/') : '.')( + fp.split('/').slice(0, -1) + ) + ) : '.'; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/takeDropCycle.md b/MD applescript vs javascript/takeDropCycle.md index 38ae41e8..34a59f75 100644 --- a/MD applescript vs javascript/takeDropCycle.md +++ b/MD applescript vs javascript/takeDropCycle.md @@ -1,13 +1,3 @@ -```javascript -// takeDropCycle :: Int -> [a] -> [a] -const takeDropCycle = n => - // N Members of an infinite cycle of xs, starting from index I - i => xs => drop(i)( - take(n + i)(cycle(xs)) - ); -``` - - ```applescript -- take N Members of an infinite cycle of xs, starting from index I -- takeDropCycle :: Int -> [a] -> [a] @@ -23,4 +13,14 @@ on takeDropCycle(n, i, xs) drop(i, take(m, ys)) end takeDropCycle +``` + + +```javascript +// takeDropCycle :: Int -> [a] -> [a] +const takeDropCycle = n => + // N Members of an infinite cycle of xs, starting from index I + i => xs => drop(i)( + take(n + i)(cycle(xs)) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/takeExtension.md b/MD applescript vs javascript/takeExtension.md index 15cf8d26..8cb4b49a 100644 --- a/MD applescript vs javascript/takeExtension.md +++ b/MD applescript vs javascript/takeExtension.md @@ -1,16 +1,3 @@ -```javascript -// takeExtension :: FilePath -> String -const takeExtension = fp => ( - fs => { - const fn = last(fs); - return fn.includes('.') ? ( - '.' + last(fn.split('.')) - ) : ''; - } -)(fp.split('/')); -``` - - ```applescript -- takeExtension :: FilePath -> String on takeExtension(strPath) @@ -21,4 +8,17 @@ on takeExtension(strPath) "" end if end takeExtension +``` + + +```javascript +// takeExtension :: FilePath -> String +const takeExtension = fp => ( + fs => { + const fn = last(fs); + return fn.includes('.') ? ( + '.' + last(fn.split('.')) + ) : ''; + } +)(fp.split('/')); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/takeFileName.md b/MD applescript vs javascript/takeFileName.md index 4058c44b..c69f28fc 100644 --- a/MD applescript vs javascript/takeFileName.md +++ b/MD applescript vs javascript/takeFileName.md @@ -1,14 +1,3 @@ -```javascript -// takeFileName :: FilePath -> FilePath -const takeFileName = fp => - '' !== fp ? ( - '/' !== fp[fp.length - 1] ? ( - fp.split('/').slice(-1)[0] - ) : '' - ) : ''; -``` - - ```applescript -- takeFileName :: FilePath -> FilePath on takeFileName(strPath) @@ -18,4 +7,15 @@ on takeFileName(strPath) "" end if end takeFileName +``` + + +```javascript +// takeFileName :: FilePath -> FilePath +const takeFileName = fp => + '' !== fp ? ( + '/' !== fp[fp.length - 1] ? ( + fp.split('/').slice(-1)[0] + ) : '' + ) : ''; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/takeFromThenTo.md b/MD applescript vs javascript/takeFromThenTo.md index a3c29c0b..78393a82 100644 --- a/MD applescript vs javascript/takeFromThenTo.md +++ b/MD applescript vs javascript/takeFromThenTo.md @@ -1,3 +1,16 @@ +```applescript +-- takeFromThenTo :: Int -> Int -> Int -> [a] -> [a] +on takeFromThenTo(a, b, z, xs) + script go + on |λ|(i) + item (1 + i) of xs + end |λ| + end script + map(go, enumFromThenTo(a, b, z)) +end takeFromThenTo +``` + + ```javascript // takeFromThenTo :: Int -> Int -> Int -> [a] -> [a] const takeFromThenTo = a => b => z => xs => { @@ -15,17 +28,4 @@ const takeFromThenTo = a => b => z => xs => { }); })(); }; -``` - - -```applescript --- takeFromThenTo :: Int -> Int -> Int -> [a] -> [a] -on takeFromThenTo(a, b, z, xs) - script go - on |λ|(i) - item (1 + i) of xs - end |λ| - end script - map(go, enumFromThenTo(a, b, z)) -end takeFromThenTo ``` \ No newline at end of file diff --git a/MD applescript vs javascript/takeIterate.md b/MD applescript vs javascript/takeIterate.md index c3dda545..f973e065 100644 --- a/MD applescript vs javascript/takeIterate.md +++ b/MD applescript vs javascript/takeIterate.md @@ -1,16 +1,3 @@ -```javascript -// takeIterate n f x == [x, f x, f (f x), ...] -// takeIterate :: Int -> (a -> a) -> a -> [a] -const takeIterate = n => f => x => - snd(mapAccumL(a => _ => i => { - const v = 0 !== i ? f(a) : x; - return [v, v]; - }, x, Array.from({ - length: n - }))); -``` - - ```applescript -- takeIterate n f x == [x, f x, f (f x), ...] -- takeIterate :: Int -> (a -> a) -> a -> [a] @@ -25,4 +12,17 @@ on takeIterate(n, f, x) end tell return vs end takeIterate +``` + + +```javascript +// takeIterate n f x == [x, f x, f (f x), ...] +// takeIterate :: Int -> (a -> a) -> a -> [a] +const takeIterate = n => f => x => + snd(mapAccumL(a => _ => i => { + const v = 0 !== i ? f(a) : x; + return [v, v]; + }, x, Array.from({ + length: n + }))); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/takeWhile.md b/MD applescript vs javascript/takeWhile.md index 2fa4297c..3c3f0b50 100644 --- a/MD applescript vs javascript/takeWhile.md +++ b/MD applescript vs javascript/takeWhile.md @@ -1,19 +1,3 @@ -```javascript -// takeWhile :: (a -> Bool) -> [a] -> [a] -// takeWhile :: (Char -> Bool) -> String -> String -const takeWhile = p => xs => - xs.constructor.constructor.name !== - 'GeneratorFunction' ? (() => { - const n = xs.length; - return xs.slice( - 0, 0 < n ? until( - i => n === i || !p(xs[i]) - )(i => 1 + i)(0) : 0 - ); - })() : takeWhileGen(p)(xs); -``` - - ```applescript -- takeWhile :: (a -> Bool) -> [a] -> [a] -- takeWhile :: (Char -> Bool) -> String -> String @@ -30,4 +14,20 @@ on takeWhile(p, xs) return xs end if end takeWhile +``` + + +```javascript +// takeWhile :: (a -> Bool) -> [a] -> [a] +// takeWhile :: (Char -> Bool) -> String -> String +const takeWhile = p => xs => + xs.constructor.constructor.name !== + 'GeneratorFunction' ? (() => { + const n = xs.length; + return xs.slice( + 0, 0 < n ? until( + i => n === i || !p(xs[i]) + )(i => 1 + i)(0) : 0 + ); + })() : takeWhileGen(p)(xs); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/takeWhileGen.md b/MD applescript vs javascript/takeWhileGen.md index 9866511a..1d54c26d 100644 --- a/MD applescript vs javascript/takeWhileGen.md +++ b/MD applescript vs javascript/takeWhileGen.md @@ -1,3 +1,19 @@ +```applescript +-- takeWhileGen :: (a -> Bool) -> Gen [a] -> [a] +on takeWhileGen(p, xs) + set ys to {} + set v to |λ|() of xs + tell mReturn(p) + repeat while (|λ|(v)) + set end of ys to v + set v to xs's |λ|() + end repeat + end tell + return ys +end takeWhileGen +``` + + ```javascript // takeWhileGen :: (a -> Bool) -> Gen [a] -> [a] const takeWhileGen = p => xs => { @@ -12,20 +28,4 @@ const takeWhileGen = p => xs => { } return ys; }; -``` - - -```applescript --- takeWhileGen :: (a -> Bool) -> Gen [a] -> [a] -on takeWhileGen(p, xs) - set ys to {} - set v to |λ|() of xs - tell mReturn(p) - repeat while (|λ|(v)) - set end of ys to v - set v to xs's |λ|() - end repeat - end tell - return ys -end takeWhileGen ``` \ No newline at end of file diff --git a/MD applescript vs javascript/takeWhileR.md b/MD applescript vs javascript/takeWhileR.md index eff521d8..d6354759 100644 --- a/MD applescript vs javascript/takeWhileR.md +++ b/MD applescript vs javascript/takeWhileR.md @@ -1,17 +1,3 @@ -```javascript -// takeWhileR :: (a -> Bool) -> [a] -> [a] -const takeWhileR = p => - // The longest suffix of xs in which - // all elements satisfy p. - xs => { - const ys = list(xs); - let i = ys.length; - while (i-- && p(ys[i])) {} - return ys.slice(i + 1); - }; -``` - - ```applescript -- takeWhileR :: (a -> Bool) -> [a] -> [a] on takeWhileR(p, xs) @@ -44,4 +30,18 @@ on takeWhileR(p, xs) xs end if end takeWhileR +``` + + +```javascript +// takeWhileR :: (a -> Bool) -> [a] -> [a] +const takeWhileR = p => + // The longest suffix of xs in which + // all elements satisfy p. + xs => { + const ys = list(xs); + let i = ys.length; + while (i-- && p(ys[i])) {} + return ys.slice(i + 1); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/taskPaperDateString.md b/MD applescript vs javascript/taskPaperDateString.md index 3a4020d6..74297495 100644 --- a/MD applescript vs javascript/taskPaperDateString.md +++ b/MD applescript vs javascript/taskPaperDateString.md @@ -1,16 +1,16 @@ -```javascript -// taskPaperDateString :: Date -> String -const taskPaperDateString = dte => { - const [d, t] = iso8601Local(dte).split('T'); - return [d, t.slice(0, 5)].join(' '); -}; -``` - - ```applescript -- taskPaperDateString :: Date -> String on taskPaperDateString(dte) set {d, t} to splitOn("T", dte as «class isot» as string) d & space & text 1 thru 5 of t end taskPaperDateString +``` + + +```javascript +// taskPaperDateString :: Date -> String +const taskPaperDateString = dte => { + const [d, t] = iso8601Local(dte).split('T'); + return [d, t.slice(0, 5)].join(' '); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/tempFilePath.md b/MD applescript vs javascript/tempFilePath.md index 0405e458..48ba953c 100644 --- a/MD applescript vs javascript/tempFilePath.md +++ b/MD applescript vs javascript/tempFilePath.md @@ -1,15 +1,3 @@ -```javascript -// tempFilePath :: String -> IO FilePath -const tempFilePath = template => - // File name template to temporary path - // Random digit sequence inserted between template base and extension - ObjC.unwrap($.NSTemporaryDirectory()) + - takeBaseName(template) + Math.random() - .toString() - .substring(3) + takeExtension(template); -``` - - ```applescript -- tempFilePath :: String -> IO FilePath on tempFilePath(template) @@ -19,4 +7,16 @@ on tempFilePath(template) text 3 thru -1 of ((random number) as string) & ¬ takeExtension(template) end tempFilePath +``` + + +```javascript +// tempFilePath :: String -> IO FilePath +const tempFilePath = template => + // File name template to temporary path + // Random digit sequence inserted between template base and extension + ObjC.unwrap($.NSTemporaryDirectory()) + + takeBaseName(template) + Math.random() + .toString() + .substring(3) + takeExtension(template); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/then (>>).md b/MD applescript vs javascript/then (>>).md index 7de1a8b7..7ed59a76 100644 --- a/MD applescript vs javascript/then (>>).md +++ b/MD applescript vs javascript/then (>>).md @@ -1,16 +1,3 @@ -```javascript -// then (>>) :: Monad m => m a -> m b -> m b -const then = ma => mb => - (Array.isArray(ma) ? ( - thenList - ) : isMaybe(ma) ? ( - thenMay - ) : thenIO)( - ...[ma, mb] - ); -``` - - ```applescript -- then (>>) :: Monad m => m a -> m b -> m b on |then|(ma, mb) @@ -27,4 +14,17 @@ on |then|(ma, mb) thenIO(ma, mb) end if end |then| +``` + + +```javascript +// then (>>) :: Monad m => m a -> m b -> m b +const then = ma => mb => + (Array.isArray(ma) ? ( + thenList + ) : isMaybe(ma) ? ( + thenMay + ) : thenIO)( + ...[ma, mb] + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/thenIO (>>).md b/MD applescript vs javascript/thenIO (>>).md index 2eacf5b5..dc8e8fa8 100644 --- a/MD applescript vs javascript/thenIO (>>).md +++ b/MD applescript vs javascript/thenIO (>>).md @@ -1,13 +1,13 @@ -```javascript -// thenIO (>>) :: IO a -> IO b -> IO b -const thenIO = ma => - mb => mb; -``` - - ```applescript -- thenIO (>>) :: IO a -> IO b -> IO b on thenIO(ma, mb) mb end thenIO +``` + + +```javascript +// thenIO (>>) :: IO a -> IO b -> IO b +const thenIO = ma => + mb => mb; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/thenList (>>).md b/MD applescript vs javascript/thenList (>>).md index da8e32d2..a1daf1bb 100644 --- a/MD applescript vs javascript/thenList (>>).md +++ b/MD applescript vs javascript/thenList (>>).md @@ -1,10 +1,3 @@ -```javascript -// thenList (>>) :: [a] -> [b] -> [b] -const thenList = xs => ys => - list(xs).flatMap(_ => list(ys)); -``` - - ```applescript -- thenList (>>) :: [a] -> [b] -> [b] on thenList(xs, ys) @@ -15,4 +8,11 @@ on thenList(xs, ys) end script concatMap(result, xs) end thenList +``` + + +```javascript +// thenList (>>) :: [a] -> [b] -> [b] +const thenList = xs => ys => + list(xs).flatMap(_ => list(ys)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/thenMay (>>).md b/MD applescript vs javascript/thenMay (>>).md index 1cad5d66..f0d883f2 100644 --- a/MD applescript vs javascript/thenMay (>>).md +++ b/MD applescript vs javascript/thenMay (>>).md @@ -1,10 +1,3 @@ -```javascript -// thenMay (>>) :: Maybe a -> Maybe b -> Maybe b -const thenMay = mbx => mby => - mbx.Nothing ? mbx : mby; -``` - - ```applescript -- thenMay (>>) :: Maybe a -> Maybe b -> Maybe b on thenMay(ma, mb) @@ -14,4 +7,11 @@ on thenMay(ma, mb) mb end if end thenMay +``` + + +```javascript +// thenMay (>>) :: Maybe a -> Maybe b -> Maybe b +const thenMay = mbx => mby => + mbx.Nothing ? mbx : mby; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/toEnum.md b/MD applescript vs javascript/toEnum.md index 8243c138..932e6782 100644 --- a/MD applescript vs javascript/toEnum.md +++ b/MD applescript vs javascript/toEnum.md @@ -1,17 +1,3 @@ -```javascript -// toEnum :: a -> Int -> a -const toEnum = e => - // The first argument is a sample of the type - // allowing the function to make the right mapping - x => ({ - 'number': Number, - 'string': String.fromCodePoint, - 'boolean': Boolean, - 'object': v => e.min + v - } [typeof e])(x); -``` - - ```applescript -- toEnum :: a -> Int -> a on toEnum(e) @@ -32,4 +18,18 @@ on toEnum(e) end |λ| end script end toEnum +``` + + +```javascript +// toEnum :: a -> Int -> a +const toEnum = e => + // The first argument is a sample of the type + // allowing the function to make the right mapping + x => ({ + 'number': Number, + 'string': String.fromCodePoint, + 'boolean': Boolean, + 'object': v => e.min + v + } [typeof e])(x); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/toLower.md b/MD applescript vs javascript/toLower.md index 0b6078f1..a2738fd9 100644 --- a/MD applescript vs javascript/toLower.md +++ b/MD applescript vs javascript/toLower.md @@ -1,11 +1,3 @@ -```javascript -// toLower :: String -> String -const toLower = s => - // Lower-case version of string. - s.toLocaleLowerCase(); -``` - - ```applescript -- toLower :: String -> String on toLower(str) @@ -14,4 +6,12 @@ on toLower(str) ((ca's NSString's stringWithString:(str))'s ¬ lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text end toLower +``` + + +```javascript +// toLower :: String -> String +const toLower = s => + // Lower-case version of string. + s.toLocaleLowerCase(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/toRatio.md b/MD applescript vs javascript/toRatio.md index cbd79399..1c37cb10 100644 --- a/MD applescript vs javascript/toRatio.md +++ b/MD applescript vs javascript/toRatio.md @@ -1,13 +1,13 @@ -```javascript -// toRatio :: Real -> Ratio -const toRatio = n => - approxRatio(1e-12)(n); -``` - - ```applescript -- toRatio :: Real -> Ratio on toRatio(n) approxRatio(1.0E-12, n) end toRatio +``` + + +```javascript +// toRatio :: Real -> Ratio +const toRatio = n => + approxRatio(1e-12)(n); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/toSentence.md b/MD applescript vs javascript/toSentence.md index 08e94538..e7f14574 100644 --- a/MD applescript vs javascript/toSentence.md +++ b/MD applescript vs javascript/toSentence.md @@ -1,15 +1,3 @@ -```javascript -// toSentence :: String -> String -const toSentence = s => - // Sentence case - initial string capitalized - // and rest lowercase. - (0 < s.length) ? ( - s[0].toUpperCase() + s.slice(1) - .toLowerCase() - ) : s; -``` - - ```applescript -- Sentence case - initial string capitalized and rest lowercase -- toSentence :: String -> String @@ -26,4 +14,16 @@ on toSentence(str) str end if end toSentence +``` + + +```javascript +// toSentence :: String -> String +const toSentence = s => + // Sentence case - initial string capitalized + // and rest lowercase. + (0 < s.length) ? ( + s[0].toUpperCase() + s.slice(1) + .toLowerCase() + ) : s; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/toTitle.md b/MD applescript vs javascript/toTitle.md index 47dbba61..7bad135e 100644 --- a/MD applescript vs javascript/toTitle.md +++ b/MD applescript vs javascript/toTitle.md @@ -1,14 +1,3 @@ -```javascript -// toTitle :: String -> String -const toTitle = s => - // NB this does not model any regional or cultural conventions. - // It simply simply capitalizes the first character of each word. - regexMatches(/(\w)(\w*)(\b[\W]*|$)/g)(s) - .map(ms => ms[1].toUpperCase() + ms[2].toLowerCase() + ms[3]) - .join(''); -``` - - ```applescript -- NB this does not model any regional or cultural conventions. -- It simply simply capitalizes the first character of each word. @@ -18,4 +7,15 @@ on toTitle(str) ((ca's NSString's stringWithString:(str))'s ¬ capitalizedStringWithLocale:(ca's NSLocale's currentLocale())) as text end toTitle +``` + + +```javascript +// toTitle :: String -> String +const toTitle = s => + // NB this does not model any regional or cultural conventions. + // It simply simply capitalizes the first character of each word. + regexMatches(/(\w)(\w*)(\b[\W]*|$)/g)(s) + .map(ms => ms[1].toUpperCase() + ms[2].toLowerCase() + ms[3]) + .join(''); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/toUpper.md b/MD applescript vs javascript/toUpper.md index 8ca2a6bb..1dba8318 100644 --- a/MD applescript vs javascript/toUpper.md +++ b/MD applescript vs javascript/toUpper.md @@ -1,10 +1,3 @@ -```javascript -// toUpper :: String -> String -const toUpper = s => - s.toLocaleUpperCase(); -``` - - ```applescript -- toUpper :: String -> String on toUpper(str) @@ -12,4 +5,11 @@ on toUpper(str) ((ca's NSString's stringWithString:(str))'s ¬ uppercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text end toUpper +``` + + +```javascript +// toUpper :: String -> String +const toUpper = s => + s.toLocaleUpperCase(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/transpose.md b/MD applescript vs javascript/transpose.md index 85f6ee77..4d399ffe 100644 --- a/MD applescript vs javascript/transpose.md +++ b/MD applescript vs javascript/transpose.md @@ -1,32 +1,3 @@ -```javascript -// transpose :: [[a]] -> [[a]] -const transpose = xss => { - // If some of the rows are shorter than the following rows, - // their elements are skipped: - // > transpose [[10,11],[20],[],[30,31,32]] == [[10,20,30],[11,31],[32]] - const go = xss => - 0 < xss.length ? (() => { - const - h = xss[0], - t = xss.slice(1); - return 0 < h.length ? [ - [h[0]].concat(t.reduce( - (a, xs) => a.concat( - 0 < xs.length ? ( - [xs[0]] - ) : [] - ), - [] - )) - ].concat(go([h.slice(1)].concat( - t.map(xs => xs.slice(1)) - ))) : go(t); - })() : []; - return go(xss); -}; -``` - - ```applescript -- If some of the rows are shorter than the following rows, -- their elements are skipped: @@ -59,4 +30,33 @@ on transpose(xxs) end script map(cols, item 1 of rows) end transpose +``` + + +```javascript +// transpose :: [[a]] -> [[a]] +const transpose = xss => { + // If some of the rows are shorter than the following rows, + // their elements are skipped: + // > transpose [[10,11],[20],[],[30,31,32]] == [[10,20,30],[11,31],[32]] + const go = xss => + 0 < xss.length ? (() => { + const + h = xss[0], + t = xss.slice(1); + return 0 < h.length ? [ + [h[0]].concat(t.reduce( + (a, xs) => a.concat( + 0 < xs.length ? ( + [xs[0]] + ) : [] + ), + [] + )) + ].concat(go([h.slice(1)].concat( + t.map(xs => xs.slice(1)) + ))) : go(t); + })() : []; + return go(xss); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/transpose_.md b/MD applescript vs javascript/transpose_.md index 36ebe334..8a732e77 100644 --- a/MD applescript vs javascript/transpose_.md +++ b/MD applescript vs javascript/transpose_.md @@ -1,3 +1,9 @@ +```applescript +-- Simplified version - assuming rows of unvarying length. +-- transpose_ :: [[a]] -> [[a]] on transpose_(rows) script cols on |λ|(_, iCol) script cell on |λ|(row) item iCol of row end |λ| end script concatMap(cell, rows) end |λ| end script map(cols, item 1 of rows) end transpose_ +``` + + ```javascript // transpose_ :: [[a]] -> [[a]] const transpose_ = rows => @@ -10,10 +16,4 @@ const transpose_ = rows => x => x[i] ) ) : []; -``` - - -```applescript --- Simplified version - assuming rows of unvarying length. --- transpose_ :: [[a]] -> [[a]] on transpose_(rows) script cols on |λ|(_, iCol) script cell on |λ|(row) item iCol of row end |λ| end script concatMap(cell, rows) end |λ| end script map(cols, item 1 of rows) end transpose_ ``` \ No newline at end of file diff --git a/MD applescript vs javascript/traverse.md b/MD applescript vs javascript/traverse.md index 4a18e197..ffd65071 100644 --- a/MD applescript vs javascript/traverse.md +++ b/MD applescript vs javascript/traverse.md @@ -1,25 +1,3 @@ -```javascript -// traverse :: (Applicative f, Traversable t) => -// (a -> f b) -> t a -> f (t b) -const traverse = f => tx => { - const t = tx.type; - return ( - undefined !== t ? ( - 'Either' === t ? ( - traverseLR - ) : 'Maybe' === t ? ( - traverseMay - ) : 'Node' === t ? ( - traverseTree - ) : 'Tuple' === t ? ( - traverseTuple - ) : traverseList - ) : traverseList - )(f)(tx); -}; -``` - - ```applescript -- traverse :: (Applicative f, Traversable t) => (a -> f b) -> t a -> f (t b) on traverse(f, tx) @@ -42,4 +20,26 @@ on traverse(f, tx) missing value end if end traverse +``` + + +```javascript +// traverse :: (Applicative f, Traversable t) => +// (a -> f b) -> t a -> f (t b) +const traverse = f => tx => { + const t = tx.type; + return ( + undefined !== t ? ( + 'Either' === t ? ( + traverseLR + ) : 'Maybe' === t ? ( + traverseMay + ) : 'Node' === t ? ( + traverseTree + ) : 'Tuple' === t ? ( + traverseTuple + ) : traverseList + ) : traverseList + )(f)(tx); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/traverseLR.md b/MD applescript vs javascript/traverseLR.md index ec0fbf62..bc3acbdc 100644 --- a/MD applescript vs javascript/traverseLR.md +++ b/MD applescript vs javascript/traverseLR.md @@ -1,3 +1,15 @@ +```applescript +-- traverseLR :: Applicative f => (t -> f b) -> Either a t -> f (Either a b) +on traverseLR(f, lr) + if |Left| of lr is not missing value then + {lr} + else + fmap(my |Right|, mReturn(f)'s |λ|(|Right| of lr)) + end if +end traverseLR +``` + + ```javascript // traverseLR :: Applicative f => // (t -> f b) -> Either a t -> f (Either a b) @@ -10,16 +22,4 @@ const traverseLR = f => ) : fmap(Right)( f(lr.Right) ); -``` - - -```applescript --- traverseLR :: Applicative f => (t -> f b) -> Either a t -> f (Either a b) -on traverseLR(f, lr) - if |Left| of lr is not missing value then - {lr} - else - fmap(my |Right|, mReturn(f)'s |λ|(|Right| of lr)) - end if -end traverseLR ``` \ No newline at end of file diff --git a/MD applescript vs javascript/traverseList.md b/MD applescript vs javascript/traverseList.md index ffaf23ae..a52e17e6 100644 --- a/MD applescript vs javascript/traverseList.md +++ b/MD applescript vs javascript/traverseList.md @@ -1,23 +1,3 @@ -```javascript -// traverseList :: (Applicative f) => (a -> f b) -> [a] -> f [b] -const traverseList = f => - // Collected results of mapping each element - // of a structure to an action, and evaluating - // these actions from left to right. - xs => ( - zs => 0 < zs.length ? (() => { - const - vLast = f(zs.slice(-1)[0]), - t = vLast.type || 'List'; - return zs.slice(0, -1).reduceRight( - (ys, z) => liftA2(cons)(f(z))(ys), - liftA2(cons)(vLast)(pureT(t)([])) - ); - })() : fType(f)([]) - )(list(xs)); -``` - - ```applescript -- 1. Map each element of a structure to an action, -- 2. evaluate these actions from left to right, and @@ -52,4 +32,24 @@ on traverseList(f, xs) {{}} end if end traverseList +``` + + +```javascript +// traverseList :: (Applicative f) => (a -> f b) -> [a] -> f [b] +const traverseList = f => + // Collected results of mapping each element + // of a structure to an action, and evaluating + // these actions from left to right. + xs => ( + zs => 0 < zs.length ? (() => { + const + vLast = f(zs.slice(-1)[0]), + t = vLast.type || 'List'; + return zs.slice(0, -1).reduceRight( + (ys, z) => liftA2(cons)(f(z))(ys), + liftA2(cons)(vLast)(pureT(t)([])) + ); + })() : fType(f)([]) + )(list(xs)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/traverseMay.md b/MD applescript vs javascript/traverseMay.md index f4e763eb..fbe271b5 100644 --- a/MD applescript vs javascript/traverseMay.md +++ b/MD applescript vs javascript/traverseMay.md @@ -1,14 +1,3 @@ -```javascript -// traverseMay :: Applicative f => (t -> f a) -> Maybe t -> f (Maybe a) -const traverseMay = f => mb => - mb.Nothing ? ( - [mb] - ) : fmap(Just)( - f(mb.Just) - ); -``` - - ```applescript -- traverseMay :: Applicative f => (t -> f a) -> Maybe t -> f (Maybe a) on traverseMay(f, mb) @@ -18,4 +7,15 @@ on traverseMay(f, mb) fmap(my Just, mReturn(f)'s |λ|(Just of mb)) end if end traverseMay +``` + + +```javascript +// traverseMay :: Applicative f => (t -> f a) -> Maybe t -> f (Maybe a) +const traverseMay = f => mb => + mb.Nothing ? ( + [mb] + ) : fmap(Just)( + f(mb.Just) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/traverseTree.md b/MD applescript vs javascript/traverseTree.md index 73266a80..98688aac 100644 --- a/MD applescript vs javascript/traverseTree.md +++ b/MD applescript vs javascript/traverseTree.md @@ -1,18 +1,3 @@ -```javascript -// traverseTree :: Applicative f => (a -> f b) -> Tree a -> f (Tree b) -const traverseTree = f => { - // traverse f (Node x ts) = liftA2 Node (f x) (traverse (traverse f) ts) - const go = tree => - liftA2(Node)(f(tree.root))( - traverseList(go)( - tree.nest - ) - ); - return go; -}; -``` - - ```applescript -- traverseTree :: Applicative f => (a -> f b) -> Tree a -> f (Tree b) on traverseTree(f, tree) @@ -26,4 +11,19 @@ on traverseTree(f, tree) end script go's |λ|(tree) end traverseTree +``` + + +```javascript +// traverseTree :: Applicative f => (a -> f b) -> Tree a -> f (Tree b) +const traverseTree = f => { + // traverse f (Node x ts) = liftA2 Node (f x) (traverse (traverse f) ts) + const go = tree => + liftA2(Node)(f(tree.root))( + traverseList(go)( + tree.nest + ) + ); + return go; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/traverseTuple.md b/MD applescript vs javascript/traverseTuple.md index bd495667..32c83033 100644 --- a/MD applescript vs javascript/traverseTuple.md +++ b/MD applescript vs javascript/traverseTuple.md @@ -1,16 +1,16 @@ -```javascript -// traverseTuple :: Functor f => (t -> f b) -> (a, t) -> f (a, b) -const traverseTuple = f => tpl => - fmap(Tuple(tpl[0]))( - f(tpl[1]) - ); -``` - - ```applescript -- traverseTuple :: Functor f => (t -> f b) -> (a, t) -> f (a, b) on traverseTuple(f, tpl) fmap(curry(my Tuple)'s |λ|(|1| of tpl), ¬ mReturn(f)'s |λ|(|2| of tpl)) end traverseTuple +``` + + +```javascript +// traverseTuple :: Functor f => (t -> f b) -> (a, t) -> f (a, b) +const traverseTuple = f => tpl => + fmap(Tuple(tpl[0]))( + f(tpl[1]) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/treeFromDict.md b/MD applescript vs javascript/treeFromDict.md index b08d55c1..dc8667fa 100644 --- a/MD applescript vs javascript/treeFromDict.md +++ b/MD applescript vs javascript/treeFromDict.md @@ -1,24 +1,3 @@ -```javascript -// treeFromDict :: String -> Dict -> Tree String -const treeFromDict = rootLabel => - dict => { - const go = x => - 'object' !== typeof x ? [] : ( - Array.isArray(x) ? ( - x.flatMap(go) - ) : keys(x).map( - k => Node(k)( - go(x[k]) - ) - ) - ); - return Node(rootLabel)( - go(dict) - ); - }; -``` - - ```applescript -- treeFromDict :: String -> Dict -> Tree String on treeFromDict(treeTitle, recDict) @@ -46,4 +25,25 @@ on treeFromDict(treeTitle, recDict) end script Node(treeTitle, go's |λ|(recDict)) end treeFromDict +``` + + +```javascript +// treeFromDict :: String -> Dict -> Tree String +const treeFromDict = rootLabel => + dict => { + const go = x => + 'object' !== typeof x ? [] : ( + Array.isArray(x) ? ( + x.flatMap(go) + ) : keys(x).map( + k => Node(k)( + go(x[k]) + ) + ) + ); + return Node(rootLabel)( + go(dict) + ); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/treeLeaves.md b/MD applescript vs javascript/treeLeaves.md index 6a280c2c..85ba1397 100644 --- a/MD applescript vs javascript/treeLeaves.md +++ b/MD applescript vs javascript/treeLeaves.md @@ -1,14 +1,3 @@ -```javascript -// treeLeaves :: Tree -> [Tree] -const treeLeaves = tree => { - const nest = tree.nest; - return (0 < nest.length) ? ( - nest.flatMap(treeLeaves) - ) : [tree]; -}; -``` - - ```applescript -- treeLeaves :: Tree -> [Tree] on treeLeaves(oNode) @@ -24,4 +13,15 @@ on treeLeaves(oNode) end script |λ|(oNode) of go end treeLeaves +``` + + +```javascript +// treeLeaves :: Tree -> [Tree] +const treeLeaves = tree => { + const nest = tree.nest; + return (0 < nest.length) ? ( + nest.flatMap(treeLeaves) + ) : [tree]; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/treeMatches.md b/MD applescript vs javascript/treeMatches.md index ee379319..ca5cc797 100644 --- a/MD applescript vs javascript/treeMatches.md +++ b/MD applescript vs javascript/treeMatches.md @@ -1,18 +1,3 @@ -```javascript -// treeMatches :: (a -> Bool) -> Tree a -> [Tree a] -const treeMatches = p => { - // A list of all nodes in the tree which match - // a predicate p. - // For the first match only, see findTree. - const go = tree => - p(tree.root) ? ( - [tree] - ) : tree.nest.flatMap(go); - return go; -}; -``` - - ```applescript -- A list of all nodes in the tree which match -- a predicate p. @@ -31,4 +16,19 @@ on treeMatches(p, tree) end script go's |λ|(tree) end treeMatches +``` + + +```javascript +// treeMatches :: (a -> Bool) -> Tree a -> [Tree a] +const treeMatches = p => { + // A list of all nodes in the tree which match + // a predicate p. + // For the first match only, see findTree. + const go = tree => + p(tree.root) ? ( + [tree] + ) : tree.nest.flatMap(go); + return go; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/treeMenu.md b/MD applescript vs javascript/treeMenu.md index bb47489d..52a925a5 100644 --- a/MD applescript vs javascript/treeMenu.md +++ b/MD applescript vs javascript/treeMenu.md @@ -1,44 +1,3 @@ -```javascript -// treeMenu :: Tree String -> IO [String] -const treeMenu = tree => { - const go = t => { - const - strTitle = t.root, - subs = t.nest, - menu = subs.map(root), - blnMore = 0 < subs.flatMap(nest).length; - return until(tpl => !fst(tpl) || !isNull(snd(tpl)))( - tpl => either( - x => Tuple(false)([]) - )( - Tuple(true) - )( - bindLR(showMenuLR(!blnMore)(strTitle)(menu))( - ks => { - const k = ks[0]; - return maybe( - Left(k + ': not found in ' + - JSON.stringify(ks) - ) - )(Right)( - bindMay(find(x => k === x.root)(subs))( - chosen => Just( - isNull(chosen.nest) ? ( - ks // Choice made in leaf menu. - ) : go(chosen) - ) - ) - ); - } - ) - ) - )(Tuple(true)([]))[1]; - }; - return go(tree); -}; -``` - - ```applescript -- treeMenu :: Tree String -> IO [String] on treeMenu(tree) @@ -99,4 +58,45 @@ on treeMenu(tree) end script |λ|(tree) of go end treeMenu +``` + + +```javascript +// treeMenu :: Tree String -> IO [String] +const treeMenu = tree => { + const go = t => { + const + strTitle = t.root, + subs = t.nest, + menu = subs.map(root), + blnMore = 0 < subs.flatMap(nest).length; + return until(tpl => !fst(tpl) || !isNull(snd(tpl)))( + tpl => either( + x => Tuple(false)([]) + )( + Tuple(true) + )( + bindLR(showMenuLR(!blnMore)(strTitle)(menu))( + ks => { + const k = ks[0]; + return maybe( + Left(k + ': not found in ' + + JSON.stringify(ks) + ) + )(Right)( + bindMay(find(x => k === x.root)(subs))( + chosen => Just( + isNull(chosen.nest) ? ( + ks // Choice made in leaf menu. + ) : go(chosen) + ) + ) + ); + } + ) + ) + )(Tuple(true)([]))[1]; + }; + return go(tree); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/truncate.md b/MD applescript vs javascript/truncate.md index 543fbce3..a2d2fb8e 100644 --- a/MD applescript vs javascript/truncate.md +++ b/MD applescript vs javascript/truncate.md @@ -1,15 +1,15 @@ +```applescript +-- truncate :: Num -> Int +on truncate(x) + item 1 of properFraction(x) +end truncate +``` + + ```javascript // truncate :: Num -> Int const truncate = x => 'Ratio' === x.type ? ( properFracRatio(x)[0] ) : properFraction(x)[0]; -``` - - -```applescript --- truncate :: Num -> Int -on truncate(x) - item 1 of properFraction(x) -end truncate ``` \ No newline at end of file diff --git a/MD applescript vs javascript/tupleFromList.md b/MD applescript vs javascript/tupleFromList.md index be106baf..3fe50b18 100644 --- a/MD applescript vs javascript/tupleFromList.md +++ b/MD applescript vs javascript/tupleFromList.md @@ -1,10 +1,3 @@ -```javascript -// tupleFromList :: [a] -> (a, a ...) -const tupleFromList = xs => - TupleN(...xs); -``` - - ```applescript -- tupleFromList :: [a] -> (a, a ...) on tupleFromList(xs) @@ -25,4 +18,11 @@ on tupleFromList(xs) missing value end if end tupleFromList +``` + + +```javascript +// tupleFromList :: [a] -> (a, a ...) +const tupleFromList = xs => + TupleN(...xs); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/typeName.md b/MD applescript vs javascript/typeName.md index 2e4985fb..c97c3940 100644 --- a/MD applescript vs javascript/typeName.md +++ b/MD applescript vs javascript/typeName.md @@ -1,23 +1,3 @@ -```javascript -// typeName :: a -> String -const typeName = v => { - const t = typeof v; - return 'object' === t ? ( - Array.isArray(v) ? ( - 'List' - ) : null !== v ? ( - v.type || 'Dict' - ) : 'Bottom' - ) : { - 'boolean': 'Bool', - 'number': 'Num', - 'string': 'String', - 'function' : '(a -> b)' - } [t] || 'Bottom'; -}; -``` - - ```applescript -- typeName :: a -> String on typeName(x) @@ -40,4 +20,24 @@ on typeName(x) end if end if end typeName +``` + + +```javascript +// typeName :: a -> String +const typeName = v => { + const t = typeof v; + return 'object' === t ? ( + Array.isArray(v) ? ( + 'List' + ) : null !== v ? ( + v.type || 'Dict' + ) : 'Bottom' + ) : { + 'boolean': 'Bool', + 'number': 'Num', + 'string': 'String', + 'function' : '(a -> b)' + } [t] || 'Bottom'; +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unDigits.md b/MD applescript vs javascript/unDigits.md new file mode 100644 index 00000000..a782747c --- /dev/null +++ b/MD applescript vs javascript/unDigits.md @@ -0,0 +1,20 @@ +```applescript +-- unDigits :: [Int] -> Int +on unDigits(ds) + -- The integer with the given digits. + script go + on |λ|(a, x) + 10 * a + x + end |λ| + end script + foldl(go, 0, ds) +end unDigits +``` + + +```javascript +// unDigits :: [Int] -> Int +const unDigits = ds => + // The integer with the given digits. + ds.reduce((a, x) => 10 * a + x, 0); +``` \ No newline at end of file diff --git a/MD applescript vs javascript/unQuoted.md b/MD applescript vs javascript/unQuoted.md index 605f7428..86925ce5 100644 --- a/MD applescript vs javascript/unQuoted.md +++ b/MD applescript vs javascript/unQuoted.md @@ -1,12 +1,3 @@ -```javascript -// unQuoted :: String -> String -const unQuoted = s => - dropAround(x => 34 === x.codePointAt(0))( - s - ); -``` - - ```applescript -- unQuoted :: String -> String on unQuoted(s) @@ -18,4 +9,13 @@ on unQuoted(s) end script dropAround(p, s) end unQuoted +``` + + +```javascript +// unQuoted :: String -> String +const unQuoted = s => + dropAround(x => 34 === x.codePointAt(0))( + s + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/uncons.md b/MD applescript vs javascript/uncons.md index 012dcfa6..3772857d 100644 --- a/MD applescript vs javascript/uncons.md +++ b/MD applescript vs javascript/uncons.md @@ -1,23 +1,3 @@ -```javascript -// uncons :: [a] -> Maybe (a, [a]) -const uncons = xs => { - // Just a tuple of the head of xs and its tail, - // Or Nothing if xs is an empty list. - const lng = length(xs); - return (0 < lng) ? ( - Infinity > lng ? ( - Just(Tuple(xs[0])(xs.slice(1))) // Finite list - ) : (() => { - const nxt = take(1)(xs); - return 0 < nxt.length ? ( - Just(Tuple(nxt[0])(xs)) - ) : Nothing(); - })() // Lazy generator - ) : Nothing(); -}; -``` - - ```applescript -- uncons :: [a] -> Maybe (a, [a]) on uncons(xs) @@ -42,4 +22,24 @@ on uncons(xs) end if end if end uncons +``` + + +```javascript +// uncons :: [a] -> Maybe (a, [a]) +const uncons = xs => { + // Just a tuple of the head of xs and its tail, + // Or Nothing if xs is an empty list. + const lng = length(xs); + return (0 < lng) ? ( + Infinity > lng ? ( + Just(Tuple(xs[0])(xs.slice(1))) // Finite list + ) : (() => { + const nxt = take(1)(xs); + return 0 < nxt.length ? ( + Just(Tuple(nxt[0])(xs)) + ) : Nothing(); + })() // Lazy generator + ) : Nothing(); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/uncurry.md b/MD applescript vs javascript/uncurry.md index 5fe1d0bc..64f9b51c 100644 --- a/MD applescript vs javascript/uncurry.md +++ b/MD applescript vs javascript/uncurry.md @@ -1,19 +1,3 @@ -```javascript -// uncurry :: (a -> b -> c) -> ((a, b) -> c) -const uncurry = f => - // A function over a pair, derived - // from a curried function. - function() { - const - args = arguments, - xy = Boolean(args.length % 2) ? ( - args[0] - ) : args; - return f(xy[0])(xy[1]); - }; -``` - - ```applescript -- Given a curried/default function, returns an -- equivalent function on a tuple or list pair. @@ -44,4 +28,20 @@ on uncurry(f) end if end uncurry +``` + + +```javascript +// uncurry :: (a -> b -> c) -> ((a, b) -> c) +const uncurry = f => + // A function over a pair, derived + // from a curried function. + function() { + const + args = arguments, + xy = Boolean(args.length % 2) ? ( + args[0] + ) : args; + return f(xy[0])(xy[1]); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unfoldForest.md b/MD applescript vs javascript/unfoldForest.md index f40538cf..6973fc4e 100644 --- a/MD applescript vs javascript/unfoldForest.md +++ b/MD applescript vs javascript/unfoldForest.md @@ -1,11 +1,3 @@ -```javascript -// unfoldForest :: (b -> (a, [b])) -> [b] -> [Tree] -const unfoldForest = f => - // A forest built from a list of seed values. - xs => xs.map(unfoldTree(f)); -``` - - ```applescript -- | Build a forest from a list of seed values -- unfoldForest :: (b -> (a, [b])) -> [b] -> [Tree] @@ -18,4 +10,12 @@ on unfoldForest(f, xs) end script map(result, xs) end unfoldForest +``` + + +```javascript +// unfoldForest :: (b -> (a, [b])) -> [b] -> [Tree] +const unfoldForest = f => + // A forest built from a list of seed values. + xs => xs.map(unfoldTree(f)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unfoldTree.md b/MD applescript vs javascript/unfoldTree.md index 1a72b647..67c4b560 100644 --- a/MD applescript vs javascript/unfoldTree.md +++ b/MD applescript vs javascript/unfoldTree.md @@ -1,3 +1,14 @@ +```applescript +-- | Build a tree from a seed value +-- unfoldTree :: (b -> (a, [b])) -> b -> Tree a +on unfoldTree(f, b) + set g to mReturn(f) + set tpl to g's |λ|(b) + Node(|1| of tpl, unfoldForest(g, |2| of tpl)) +end unfoldTree +``` + + ```javascript // unfoldTree :: (b -> (a, [b])) -> b -> Tree a const unfoldTree = f => @@ -11,15 +22,4 @@ const unfoldTree = f => f(b) ) ); -``` - - -```applescript --- | Build a tree from a seed value --- unfoldTree :: (b -> (a, [b])) -> b -> Tree a -on unfoldTree(f, b) - set g to mReturn(f) - set tpl to g's |λ|(b) - Node(|1| of tpl, unfoldForest(g, |2| of tpl)) -end unfoldTree ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unfoldl.md b/MD applescript vs javascript/unfoldl.md index 2ad48860..8b557889 100644 --- a/MD applescript vs javascript/unfoldl.md +++ b/MD applescript vs javascript/unfoldl.md @@ -1,3 +1,27 @@ +```applescript +-- > unfoldl (\b -> if b == 0 then Nothing else Just (b, b-1)) 10 +-- > [1,2,3,4,5,6,7,8,9,10] +-- unfoldl :: (b -> Maybe (b, a)) -> b -> [a] +on unfoldl(f, v) + set xr to Tuple(v, v) -- (value, remainder) + set xs to {} + tell mReturn(f) + repeat -- Function applied to remainder. + set mb to |λ|(|2| of xr) + if Nothing of mb then + exit repeat + else -- New (value, remainder) tuple, + set xr to Just of mb + -- and value appended to output list. + set xs to ({|1| of xr} & xs) + end if + end repeat + end tell + return xs +end unfoldl +``` + + ```javascript // unfoldl :: (b -> Maybe (b, a)) -> b -> [a] const unfoldl = f => v => { @@ -23,28 +47,4 @@ const unfoldl = f => v => { } } }; -``` - - -```applescript --- > unfoldl (\b -> if b == 0 then Nothing else Just (b, b-1)) 10 --- > [1,2,3,4,5,6,7,8,9,10] --- unfoldl :: (b -> Maybe (b, a)) -> b -> [a] -on unfoldl(f, v) - set xr to Tuple(v, v) -- (value, remainder) - set xs to {} - tell mReturn(f) - repeat -- Function applied to remainder. - set mb to |λ|(|2| of xr) - if Nothing of mb then - exit repeat - else -- New (value, remainder) tuple, - set xr to Just of mb - -- and value appended to output list. - set xs to ({|1| of xr} & xs) - end if - end repeat - end tell - return xs -end unfoldl ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unfoldr.md b/MD applescript vs javascript/unfoldr.md index 2392e228..2af081ee 100644 --- a/MD applescript vs javascript/unfoldr.md +++ b/MD applescript vs javascript/unfoldr.md @@ -1,3 +1,29 @@ +```applescript +-- unfoldr :: (b -> Maybe (a, b)) -> b -> [a] +on unfoldr(f, v) + -- A list obtained from a simple value. + -- Dual to foldr. + -- unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10 + -- -> [10,9,8,7,6,5,4,3,2,1] + set xr to {v, v} -- (value, remainder) + set xs to {} + tell mReturn(f) + repeat -- Function applied to remainder. + set mb to |λ|(snd(xr)) + if Nothing of mb then + exit repeat + else -- New (value, remainder) tuple, + set xr to Just of mb + -- and value appended to output list. + set end of xs to fst(xr) + end if + end repeat + end tell + return xs +end unfoldr +``` + + ```javascript // The 'unfoldr' function is a *dual* to 'foldr': while 'foldr' // reduces a list to a summary value, 'unfoldr' builds a list from @@ -24,30 +50,4 @@ const unfoldr = f => } } }; -``` - - -```applescript --- unfoldr :: (b -> Maybe (a, b)) -> b -> [a] -on unfoldr(f, v) - -- A list obtained from a simple value. - -- Dual to foldr. - -- unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10 - -- -> [10,9,8,7,6,5,4,3,2,1] - set xr to {v, v} -- (value, remainder) - set xs to {} - tell mReturn(f) - repeat -- Function applied to remainder. - set mb to |λ|(snd(xr)) - if Nothing of mb then - exit repeat - else -- New (value, remainder) tuple, - set xr to Just of mb - -- and value appended to output list. - set end of xs to fst(xr) - end if - end repeat - end tell - return xs -end unfoldr ``` \ No newline at end of file diff --git a/MD applescript vs javascript/union.md b/MD applescript vs javascript/union.md index d050017e..d9f9d8e8 100644 --- a/MD applescript vs javascript/union.md +++ b/MD applescript vs javascript/union.md @@ -1,12 +1,3 @@ -```javascript -// union :: [a] -> [a] -> [a] -const union = xs => ys => - unionBy(a => b => a === b)( - list(xs) - )(list(ys)); -``` - - ```applescript -- union :: [a] -> [a] -> [a] on union(xs, ys) @@ -19,4 +10,13 @@ on union(xs, ys) set sx to nub(xs) sx & foldl(flipDelete, nub(ys), sx) end union +``` + + +```javascript +// union :: [a] -> [a] -> [a] +const union = xs => ys => + unionBy(a => b => a === b)( + list(xs) + )(list(ys)); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unionBy.md b/MD applescript vs javascript/unionBy.md index da4cdc5a..a397c0af 100644 --- a/MD applescript vs javascript/unionBy.md +++ b/MD applescript vs javascript/unionBy.md @@ -1,3 +1,16 @@ +```applescript +-- unionBy :: (a -> a -> Bool) -> [a] -> [a] -> [a] +on unionBy(fnEq, xs, ys) + script flipDeleteByEq + on |λ|(xs, x) + deleteBy(fnEq, x, xs) + end |λ| + end script + xs & foldl(flipDeleteByEq, nubBy(fnEq, ys), xs) +end unionBy +``` + + ```javascript // unionBy :: (a -> a -> Bool) -> [a] -> [a] -> [a] const unionBy = fnEq => xs => ys => { @@ -11,17 +24,4 @@ const unionBy = fnEq => xs => ys => { ) ); }; -``` - - -```applescript --- unionBy :: (a -> a -> Bool) -> [a] -> [a] -> [a] -on unionBy(fnEq, xs, ys) - script flipDeleteByEq - on |λ|(xs, x) - deleteBy(fnEq, x, xs) - end |λ| - end script - xs & foldl(flipDeleteByEq, nubBy(fnEq, ys), xs) -end unionBy ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unionSet.md b/MD applescript vs javascript/unionSet.md index 47196a60..f7b3454d 100644 --- a/MD applescript vs javascript/unionSet.md +++ b/MD applescript vs javascript/unionSet.md @@ -1,14 +1,3 @@ -```javascript -// unionSet :: Ord a => Set a -> Set a -> Set a -const unionSet = s => s1 => - Array.from(s1.values()) - .reduce( - (a, x) => (a.add(x), a), - new Set(s) - ); -``` - - ```applescript -- unionSet :: Ord a => Set a -> Set a -> Set a on unionSet(s, s1) @@ -17,4 +6,15 @@ on unionSet(s, s1) sUnion's unionSet:(s1) return sUnion end unionSet +``` + + +```javascript +// unionSet :: Ord a => Set a -> Set a -> Set a +const unionSet = s => s1 => + Array.from(s1.values()) + .reduce( + (a, x) => (a.add(x), a), + new Set(s) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unlines.md b/MD applescript vs javascript/unlines.md index cc46fed1..7671ed14 100644 --- a/MD applescript vs javascript/unlines.md +++ b/MD applescript vs javascript/unlines.md @@ -1,12 +1,3 @@ -```javascript -// unlines :: [String] -> String -const unlines = xs => - // A single string formed by the intercalation - // of a list of strings with the newline character. - xs.join('\n'); -``` - - ```applescript -- unlines :: [String] -> String on unlines(xs) @@ -18,4 +9,13 @@ on unlines(xs) set my text item delimiters to dlm s end unlines +``` + + +```javascript +// unlines :: [String] -> String +const unlines = xs => + // A single string formed by the intercalation + // of a list of strings with the newline character. + xs.join('\n'); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unsnoc.md b/MD applescript vs javascript/unsnoc.md index 9a96323b..9c591ead 100644 --- a/MD applescript vs javascript/unsnoc.md +++ b/MD applescript vs javascript/unsnoc.md @@ -1,14 +1,3 @@ -```javascript -// unsnoc :: [a] -> Maybe ([a], a) -const unsnoc = xs => - // Nothing if the list is empty, otherwise - // Just the init and the last. - (0 < xs.length) ? ( - Just(Tuple(xs.slice(0, -1))(xs.slice(-1)[0])) - ) : Nothing(); -``` - - ```applescript -- If the list is empty returns Nothing, otherwise returns -- Just the init and the last. @@ -35,4 +24,15 @@ on unsnoc(xs) end if end if end unsnoc +``` + + +```javascript +// unsnoc :: [a] -> Maybe ([a], a) +const unsnoc = xs => + // Nothing if the list is empty, otherwise + // Just the init and the last. + (0 < xs.length) ? ( + Just(Tuple(xs.slice(0, -1))(xs.slice(-1)[0])) + ) : Nothing(); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/until.md b/MD applescript vs javascript/until.md index 4a37c411..d8c60b9e 100644 --- a/MD applescript vs javascript/until.md +++ b/MD applescript vs javascript/until.md @@ -1,3 +1,8 @@ +```applescript +-- until :: (a -> Bool) -> (a -> a) -> a -> a on |until|(p, f, x) set v to x set mp to mReturn(p) set mf to mReturn(f) repeat until mp's |λ|(v) set v to mf's |λ|(v) end repeat v end |until| +``` + + ```javascript // until :: (a -> Bool) -> (a -> a) -> a -> a const until = p => @@ -6,9 +11,4 @@ const until = p => while (!p(v)) v = f(v); return v; }; -``` - - -```applescript --- until :: (a -> Bool) -> (a -> a) -> a -> a on |until|(p, f, x) set v to x set mp to mReturn(p) set mf to mReturn(f) repeat until mp's |λ|(v) set v to mf's |λ|(v) end repeat v end |until| ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unwords.md b/MD applescript vs javascript/unwords.md index 29a1c73e..d2c23f1a 100644 --- a/MD applescript vs javascript/unwords.md +++ b/MD applescript vs javascript/unwords.md @@ -1,12 +1,3 @@ -```javascript -// unwords :: [String] -> String -const unwords = xs => - // A space-separated string derived - // from a list of words. - xs.join(' '); -``` - - ```applescript -- unwords :: [String] -> String on unwords(xs) @@ -16,4 +7,13 @@ on unwords(xs) set my text item delimiters to dlm return s end unwords +``` + + +```javascript +// unwords :: [String] -> String +const unwords = xs => + // A space-separated string derived + // from a list of words. + xs.join(' '); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unwrap.md b/MD applescript vs javascript/unwrap.md index a876dc6d..b24026a7 100644 --- a/MD applescript vs javascript/unwrap.md +++ b/MD applescript vs javascript/unwrap.md @@ -1,9 +1,3 @@ -```javascript -// unwrap :: NSObject -> a -const unwrap = ObjC.unwrap; -``` - - ```applescript -- unwrap :: NSObject -> a on unwrap(objCValue) @@ -14,4 +8,10 @@ on unwrap(objCValue) item 1 of ((ca's NSArray's arrayWithObject:objCValue) as list) end if end unwrap +``` + + +```javascript +// unwrap :: NSObject -> a +const unwrap = ObjC.unwrap; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unzip.md b/MD applescript vs javascript/unzip.md index ffdfd7fe..485b32ba 100644 --- a/MD applescript vs javascript/unzip.md +++ b/MD applescript vs javascript/unzip.md @@ -1,15 +1,3 @@ -```javascript -// unzip :: [(a,b)] -> ([a],[b]) -const unzip = xys => - xys.reduce( - (ab, xy) => Tuple(ab[0].concat(xy[0]))( - ab[1].concat(xy[1]) - ), - Tuple([])([]) - ); -``` - - ```applescript -- unzip :: [(a,b)] -> ([a],[b]) on unzip(xys) @@ -21,4 +9,16 @@ on unzip(xys) end repeat return Tuple(xs, ys) end unzip +``` + + +```javascript +// unzip :: [(a,b)] -> ([a],[b]) +const unzip = xys => + xys.reduce( + (ab, xy) => Tuple(ab[0].concat(xy[0]))( + ab[1].concat(xy[1]) + ), + Tuple([])([]) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unzip3.md b/MD applescript vs javascript/unzip3.md index 8e4f4fe9..a427f83d 100644 --- a/MD applescript vs javascript/unzip3.md +++ b/MD applescript vs javascript/unzip3.md @@ -1,15 +1,3 @@ -```javascript -// unzip3 :: [(a,b,c)] -> ([a],[b],[c]) -const unzip3 = xyzs => - xyzs.reduce( - (a, x) => TupleN.apply(null, [0, 1, 2].map( - i => a[i].concat(x[i]) - )), - TupleN([], [], []) - ); -``` - - ```applescript -- unzip3 :: [(a,b,c)] -> ([a],[b],[c]) on unzip3(xyzs) @@ -23,4 +11,16 @@ on unzip3(xyzs) end repeat return TupleN({xs, ys, zs}) end unzip3 +``` + + +```javascript +// unzip3 :: [(a,b,c)] -> ([a],[b],[c]) +const unzip3 = xyzs => + xyzs.reduce( + (a, x) => TupleN.apply(null, [0, 1, 2].map( + i => a[i].concat(x[i]) + )), + TupleN([], [], []) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unzip4.md b/MD applescript vs javascript/unzip4.md index 32fb6df5..186f5869 100644 --- a/MD applescript vs javascript/unzip4.md +++ b/MD applescript vs javascript/unzip4.md @@ -1,15 +1,3 @@ -```javascript -// unzip4 :: [(a,b,c,d)] -> ([a],[b],[c],[d]) -const unzip4 = wxyzs => - wxyzs.reduce( - (a, x) => TupleN.apply(null, [0, 1, 2, 3].map( - i => a[i].concat(x[i]) - )), - TupleN([], [], [], []) - ); -``` - - ```applescript -- unzip4 :: [(a,b,c,d)] -> ([a],[b],[c],[d]) on unzip4(wxyzs) @@ -25,4 +13,16 @@ on unzip4(wxyzs) end repeat return TupleN({ws, xs, ys, zs}) end unzip4 +``` + + +```javascript +// unzip4 :: [(a,b,c,d)] -> ([a],[b],[c],[d]) +const unzip4 = wxyzs => + wxyzs.reduce( + (a, x) => TupleN.apply(null, [0, 1, 2, 3].map( + i => a[i].concat(x[i]) + )), + TupleN([], [], [], []) + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/unzipN.md b/MD applescript vs javascript/unzipN.md index bb40121a..0b8c1885 100644 --- a/MD applescript vs javascript/unzipN.md +++ b/MD applescript vs javascript/unzipN.md @@ -1,19 +1,3 @@ -```javascript -// unzipN :: [(a,b,...)] -> ([a],[b],...) -const unzipN = tpls => - TupleN(...tpls.reduce( - (a, tpl) => a.map( - (x, i) => x.concat(tpl[i]) - ), - replicate( - 0 < tpls.length ? ( - tpls[0].length - ) : 0, [] - ) - )); -``` - - ```applescript -- unzipN :: [(a,b,...)] -> ([a],[b],...) on unzipN(tpls) @@ -34,4 +18,20 @@ on unzipN(tpls) missing value end if end unzipN +``` + + +```javascript +// unzipN :: [(a,b,...)] -> ([a],[b],...) +const unzipN = tpls => + TupleN(...tpls.reduce( + (a, tpl) => a.map( + (x, i) => x.concat(tpl[i]) + ), + replicate( + 0 < tpls.length ? ( + tpls[0].length + ) : 0, [] + ) + )); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/variance.md b/MD applescript vs javascript/variance.md index fd086a6d..b89b746b 100644 --- a/MD applescript vs javascript/variance.md +++ b/MD applescript vs javascript/variance.md @@ -1,17 +1,3 @@ -```javascript -// variance :: [Num] -> Num -const variance = xs => { - const - lng = xs.length, - mean = xs.reduce((a, b) => a + b, 0) / lng; - return xs.reduce( - (a, b) => a + Math.pow(b - mean, 2), - 0 - ) / (lng - 1); -}; -``` - - ```applescript -- variance :: [Num] -> Num on variance(xs) @@ -23,4 +9,18 @@ on variance(xs) end script foldl(result, 0, xs) / ((length of xs) - 1) end variance +``` + + +```javascript +// variance :: [Num] -> Num +const variance = xs => { + const + lng = xs.length, + mean = xs.reduce((a, b) => a + b, 0) / lng; + return xs.reduce( + (a, b) => a + Math.pow(b - mean, 2), + 0 + ) / (lng - 1); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/words.md b/MD applescript vs javascript/words.md index 82f89476..fd735a96 100644 --- a/MD applescript vs javascript/words.md +++ b/MD applescript vs javascript/words.md @@ -1,11 +1,3 @@ -```javascript -// words :: String -> [String] -const words = s => - // List of space-delimited sub-strings. - s.split(/\s+/); -``` - - ```applescript -- words :: String -> [String] on |words|(s) @@ -16,4 +8,12 @@ on |words|(s) filteredArrayUsingPredicate:(ca's ¬ NSPredicate's predicateWithFormat:"0 < length")) as list end |words| +``` + + +```javascript +// words :: String -> [String] +const words = s => + // List of space-delimited sub-strings. + s.split(/\s+/); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/wrap.md b/MD applescript vs javascript/wrap.md index 6d891899..3d975727 100644 --- a/MD applescript vs javascript/wrap.md +++ b/MD applescript vs javascript/wrap.md @@ -1,13 +1,13 @@ -```javascript -// wrap :: a -> NSObject -const wrap = ObjC.wrap; -``` - - ```applescript -- wrap :: a -> NSObject on wrap(v) set ca to current application ca's (NSArray's arrayWithObject:v)'s objectAtIndex:0 end wrap +``` + + +```javascript +// wrap :: a -> NSObject +const wrap = ObjC.wrap; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/writeFile.md b/MD applescript vs javascript/writeFile.md index b82f63b3..fdf1ce0b 100644 --- a/MD applescript vs javascript/writeFile.md +++ b/MD applescript vs javascript/writeFile.md @@ -1,15 +1,3 @@ -```javascript -// writeFile :: FilePath -> String -> IO () -const writeFile = fp => s => - $.NSString.alloc.initWithUTF8String(s) - .writeToFileAtomicallyEncodingError( - $(fp) - .stringByStandardizingPath, false, - $.NSUTF8StringEncoding, null - ); -``` - - ```applescript -- use framework "Foundation" -- writeFile :: FilePath -> String -> IO () @@ -20,4 +8,16 @@ on writeFile(strPath, strText) (ca's NSString's stringWithString:strPath)) atomically:true ¬ encoding:(ca's NSUTF8StringEncoding) |error|:(missing value) end writeFile +``` + + +```javascript +// writeFile :: FilePath -> String -> IO () +const writeFile = fp => s => + $.NSString.alloc.initWithUTF8String(s) + .writeToFileAtomicallyEncodingError( + $(fp) + .stringByStandardizingPath, false, + $.NSUTF8StringEncoding, null + ); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/writeFileLR.md b/MD applescript vs javascript/writeFileLR.md index 25dc00a7..9c691971 100644 --- a/MD applescript vs javascript/writeFileLR.md +++ b/MD applescript vs javascript/writeFileLR.md @@ -1,3 +1,21 @@ +```applescript +-- writeFileLR :: FilePath -> Either String IO FilePath +on writeFileLR(strPath, strText) + set ca to current application + set fp to stringByStandardizingPath of ¬ + (ca's NSString's stringWithString:strPath) + set {bln, e} to (ca's NSString's stringWithString:strText)'s ¬ + writeToFile:(fp) atomically:true ¬ + encoding:(ca's NSUTF8StringEncoding) |error|:(reference) + if bln and (missing value is e) then + |Right|(fp as string) + else + |Left|(e's localizedDescription() as string) + end if +end writeFileLR +``` + + ```javascript // writeFileLR :: FilePath -> Either String IO FilePath const writeFileLR = fp => @@ -14,22 +32,4 @@ const writeFileLR = fp => Right(ObjC.unwrap(efp)) ) : Left(ObjC.unwrap(e.localizedDescription)); }; -``` - - -```applescript --- writeFileLR :: FilePath -> Either String IO FilePath -on writeFileLR(strPath, strText) - set ca to current application - set fp to stringByStandardizingPath of ¬ - (ca's NSString's stringWithString:strPath) - set {bln, e} to (ca's NSString's stringWithString:strText)'s ¬ - writeToFile:(fp) atomically:true ¬ - encoding:(ca's NSUTF8StringEncoding) |error|:(reference) - if bln and (missing value is e) then - |Right|(fp as string) - else - |Left|(e's localizedDescription() as string) - end if -end writeFileLR ``` \ No newline at end of file diff --git a/MD applescript vs javascript/writeTempFile.md b/MD applescript vs javascript/writeTempFile.md index bcfe223b..4cd54970 100644 --- a/MD applescript vs javascript/writeTempFile.md +++ b/MD applescript vs javascript/writeTempFile.md @@ -1,18 +1,3 @@ -```javascript -// writeTempFile :: String -> String -> IO FilePath -const writeTempFile = template => - // File name template -> string data -> IO temporary path - txt => { - const - strPath = ObjC.unwrap($.NSTemporaryDirectory()) + - takeBaseName(template) + Math.random() - .toString() - .substring(3) + takeExtension(template); - return (writeFile(strPath)(txt), strPath); - }; -``` - - ```applescript use framework "Foundation" -- File name template -> string data -> temporary path @@ -29,4 +14,19 @@ on writeTempFile(template, txt) -- Value strPath end writeTempFile +``` + + +```javascript +// writeTempFile :: String -> String -> IO FilePath +const writeTempFile = template => + // File name template -> string data -> IO temporary path + txt => { + const + strPath = ObjC.unwrap($.NSTemporaryDirectory()) + + takeBaseName(template) + Math.random() + .toString() + .substring(3) + takeExtension(template); + return (writeFile(strPath)(txt), strPath); + }; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/zip.md b/MD applescript vs javascript/zip.md index cd8a8e87..ddce8290 100644 --- a/MD applescript vs javascript/zip.md +++ b/MD applescript vs javascript/zip.md @@ -1,3 +1,11 @@ +```applescript +-- zip :: [a] -> [b] -> [(a, b)] +on zip(xs, ys) + zipWith(Tuple, xs, ys) +end zip +``` + + ```javascript // zip :: [a] -> [b] -> [(a, b)] const zip = xs => ys => @@ -6,12 +14,4 @@ const zip = xs => ys => Array.from({ length: Math.min(xs.length, ys.length) }, (_, i) => Tuple(xs[i])(ys[i])); -``` - - -```applescript --- zip :: [a] -> [b] -> [(a, b)] -on zip(xs, ys) - zipWith(Tuple, xs, ys) -end zip ``` \ No newline at end of file diff --git a/MD applescript vs javascript/zip3.md b/MD applescript vs javascript/zip3.md index edf5fe61..647d1046 100644 --- a/MD applescript vs javascript/zip3.md +++ b/MD applescript vs javascript/zip3.md @@ -1,12 +1,3 @@ -```javascript -// zip3 :: [a] -> [b] -> [c] -> [(a, b, c)] -const zip3 = xs => - ys => zs => list(xs) - .slice(0, Math.min(...[xs, ys, zs].map(length))) - .map((x, i) => TupleN(x, ys[i], zs[i])); -``` - - ```applescript -- zip3 :: [a] -> [b] -> [c] -> [(a, b, c)] on zip3(xs, ys, zs) @@ -18,4 +9,13 @@ on zip3(xs, ys, zs) map(result, items 1 thru ¬ minimum({length of xs, length of ys, length of zs}) of xs) end zip3 +``` + + +```javascript +// zip3 :: [a] -> [b] -> [c] -> [(a, b, c)] +const zip3 = xs => + ys => zs => list(xs) + .slice(0, Math.min(...[xs, ys, zs].map(length))) + .map((x, i) => TupleN(x, ys[i], zs[i])); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/zip4.md b/MD applescript vs javascript/zip4.md index f8610134..da17dcd2 100644 --- a/MD applescript vs javascript/zip4.md +++ b/MD applescript vs javascript/zip4.md @@ -1,12 +1,3 @@ -```javascript -// zip4 :: [a] -> [b] -> [c] -> [d] -> [(a, b, c, d)] -const zip4 = ws => - xs => ys => zs => list(ws) - .slice(0, Math.min(...[ws, xs, ys, zs].map(length))) - .map((w, i) => TupleN(w, xs[i], ys[i], zs[i])); -``` - - ```applescript -- zip4 :: [a] -> [b] -> [c] -> [d] -> [(a, b, c, d)] on zip4(ws, xs, ys, zs) @@ -18,4 +9,13 @@ on zip4(ws, xs, ys, zs) map(result, items 1 thru ¬ minimum({length of xs, length of ys, length of zs}) of xs) end zip4 +``` + + +```javascript +// zip4 :: [a] -> [b] -> [c] -> [d] -> [(a, b, c, d)] +const zip4 = ws => + xs => ys => zs => list(ws) + .slice(0, Math.min(...[ws, xs, ys, zs].map(length))) + .map((w, i) => TupleN(w, xs[i], ys[i], zs[i])); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/zipGen.md b/MD applescript vs javascript/zipGen.md index a28a7621..215d74d2 100644 --- a/MD applescript vs javascript/zipGen.md +++ b/MD applescript vs javascript/zipGen.md @@ -1,28 +1,3 @@ -```javascript -// zipGen :: Gen [a] -> Gen [b] -> Gen [(a, b)] -const zipGen = ga => gb => { - function* go(ma, mb) { - let - a = ma, - b = mb; - while(!a.Nothing && !b.Nothing) { - let - ta = a.Just, - tb = b.Just; - yield( - Tuple(fst(ta))( - fst(tb) - ) - ); - a = uncons(snd(ta)); - b = uncons(snd(tb)); - } - } - return go(uncons(ga), uncons(gb)); -}; -``` - - ```applescript -- zipGen :: Gen [a] -> Gen [b] -> Gen [(a, b)] on zipGen(ga, gb) @@ -47,4 +22,29 @@ on zipGen(ga, gb) end |λ| end script end zipGen +``` + + +```javascript +// zipGen :: Gen [a] -> Gen [b] -> Gen [(a, b)] +const zipGen = ga => gb => { + function* go(ma, mb) { + let + a = ma, + b = mb; + while(!a.Nothing && !b.Nothing) { + let + ta = a.Just, + tb = b.Just; + yield( + Tuple(fst(ta))( + fst(tb) + ) + ); + a = uncons(snd(ta)); + b = uncons(snd(tb)); + } + } + return go(uncons(ga), uncons(gb)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/zipList.md b/MD applescript vs javascript/zipList.md index cf930b1d..5396737c 100644 --- a/MD applescript vs javascript/zipList.md +++ b/MD applescript vs javascript/zipList.md @@ -1,15 +1,3 @@ -```javascript -// zipList :: [a] -> [b] -> [(a, b)] -const zipList = xs => ys => { - const - n = Math.min(length(xs), length(ys)), - vs = take(n)(list(ys)); - return take(n)(list(xs)) - .map((x, i) => Tuple(x)(vs[i])); -}; -``` - - ```applescript -- zipList :: [a] -> [b] -> [(a, b)] on zipList(xs, ys) @@ -21,4 +9,16 @@ on zipList(xs, ys) end script map(go, items 1 thru lng of xs) end zipList +``` + + +```javascript +// zipList :: [a] -> [b] -> [(a, b)] +const zipList = xs => ys => { + const + n = Math.min(length(xs), length(ys)), + vs = take(n)(list(ys)); + return take(n)(list(xs)) + .map((x, i) => Tuple(x)(vs[i])); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/zipN.md b/MD applescript vs javascript/zipN.md index 18157c93..488ca032 100644 --- a/MD applescript vs javascript/zipN.md +++ b/MD applescript vs javascript/zipN.md @@ -1,18 +1,3 @@ -```javascript -// zipN :: [a] -> [b] -> ... -> [(a, b ...)] -function zipN() { - const args = Array.from(arguments).map(list); - return 1 < args.length ? ( - take( - Math.min(...args.map(length)) - )(args[0]).map( - (x, i) => TupleN(...args.map(y => y[i])) - ) - ) : args; -} -``` - - ```applescript -- Arbitrary number of lists to zip -- all enclosed in an argument vector list @@ -35,4 +20,19 @@ on zipN(argv) argv end if end zipN +``` + + +```javascript +// zipN :: [a] -> [b] -> ... -> [(a, b ...)] +function zipN() { + const args = Array.from(arguments).map(list); + return 1 < args.length ? ( + take( + Math.min(...args.map(length)) + )(args[0]).map( + (x, i) => TupleN(...args.map(y => y[i])) + ) + ) : args; +} ``` \ No newline at end of file diff --git a/MD applescript vs javascript/zipWith.md b/MD applescript vs javascript/zipWith.md index c0ef4b19..683bede7 100644 --- a/MD applescript vs javascript/zipWith.md +++ b/MD applescript vs javascript/zipWith.md @@ -1,3 +1,21 @@ +```applescript +-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] +on zipWith(f, xs, ys) + set lng to min(|length|(xs), |length|(ys)) + if 1 > lng then return {} + set xs_ to take(lng, xs) -- Allow for non-finite + set ys_ to take(lng, ys) -- generators like cycle etc + set lst to {} + tell mReturn(f) + repeat with i from 1 to lng + set end of lst to |λ|(item i of xs_, item i of ys_) + end repeat + return lst + end tell +end zipWith +``` + + ```javascript // zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] const zipWith = f => @@ -16,22 +34,4 @@ const zipWith = f => )) ) : zipWithGen(f)(xs)(ys); }; -``` - - -```applescript --- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] -on zipWith(f, xs, ys) - set lng to min(|length|(xs), |length|(ys)) - if 1 > lng then return {} - set xs_ to take(lng, xs) -- Allow for non-finite - set ys_ to take(lng, ys) -- generators like cycle etc - set lst to {} - tell mReturn(f) - repeat with i from 1 to lng - set end of lst to |λ|(item i of xs_, item i of ys_) - end repeat - return lst - end tell -end zipWith ``` \ No newline at end of file diff --git a/MD applescript vs javascript/zipWith3.md b/MD applescript vs javascript/zipWith3.md index 99fa3d5b..17cf26f2 100644 --- a/MD applescript vs javascript/zipWith3.md +++ b/MD applescript vs javascript/zipWith3.md @@ -1,14 +1,3 @@ -```javascript -// zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] -const zipWith3 = f => - xs => ys => zs => Array.from({ - length: Math.min( - ...[xs, ys, zs].map(x => x.length) - ) - }, (_, i) => f(xs[i])(ys[i])(zs[i])); -``` - - ```applescript -- zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] on zipWith3(f, xs, ys, zs) @@ -22,4 +11,15 @@ on zipWith3(f, xs, ys, zs) return lst end tell end zipWith3 +``` + + +```javascript +// zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] +const zipWith3 = f => + xs => ys => zs => Array.from({ + length: Math.min( + ...[xs, ys, zs].map(x => x.length) + ) + }, (_, i) => f(xs[i])(ys[i])(zs[i])); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/zipWith4.md b/MD applescript vs javascript/zipWith4.md index 0eaccf32..28789305 100644 --- a/MD applescript vs javascript/zipWith4.md +++ b/MD applescript vs javascript/zipWith4.md @@ -1,14 +1,3 @@ -```javascript -// zipWith4 :: (a -> b -> c -> d -> e) -> [a] -> [b] -> [c] -> [d] -> [e] -const zipWith4 = f => - ws => xs => ys => zs => Array.from({ - length: Math.min( - ...[ws, xs, ys, zs].map(x => x.length) - ) - }, (_, i) => f(ws[i])(xs[i])(ys[i])(zs[i])); -``` - - ```applescript -- zipWith4 :: (a -> b -> c -> d -> e) -> [a] -> [b] -> [c] -> [d] -> [e] on zipWith4(f, ws, xs, ys, zs) @@ -22,4 +11,15 @@ on zipWith4(f, ws, xs, ys, zs) return lst end tell end zipWith4 +``` + + +```javascript +// zipWith4 :: (a -> b -> c -> d -> e) -> [a] -> [b] -> [c] -> [d] -> [e] +const zipWith4 = f => + ws => xs => ys => zs => Array.from({ + length: Math.min( + ...[ws, xs, ys, zs].map(x => x.length) + ) + }, (_, i) => f(ws[i])(xs[i])(ys[i])(zs[i])); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/zipWithGen.md b/MD applescript vs javascript/zipWithGen.md index b3953eb7..8446af65 100644 --- a/MD applescript vs javascript/zipWithGen.md +++ b/MD applescript vs javascript/zipWithGen.md @@ -1,25 +1,3 @@ -```javascript -// zipWithGen :: (a -> b -> c) -> -// Gen [a] -> Gen [b] -> Gen [c] -const zipWithGen = f => ga => gb => { - function* go(ma, mb) { - let - a = ma, - b = mb; - while (!a.Nothing && !b.Nothing) { - let - ta = a.Just, - tb = b.Just; - yield(f(fst(ta))(fst(tb))); - a = uncons(snd(ta)); - b = uncons(snd(tb)); - } - } - return go(uncons(ga), uncons(gb)); -}; -``` - - ```applescript -- zipWithGen :: (a -> b -> c) -> Gen [a] -> Gen [b] -> Gen [c] on zipWithGen(f, ga, gb) @@ -44,4 +22,26 @@ on zipWithGen(f, ga, gb) end |λ| end script end zipWithGen +``` + + +```javascript +// zipWithGen :: (a -> b -> c) -> +// Gen [a] -> Gen [b] -> Gen [c] +const zipWithGen = f => ga => gb => { + function* go(ma, mb) { + let + a = ma, + b = mb; + while (!a.Nothing && !b.Nothing) { + let + ta = a.Just, + tb = b.Just; + yield(f(fst(ta))(fst(tb))); + a = uncons(snd(ta)); + b = uncons(snd(tb)); + } + } + return go(uncons(ga), uncons(gb)); +}; ``` \ No newline at end of file diff --git a/MD applescript vs javascript/zipWithList.md b/MD applescript vs javascript/zipWithList.md index 79c1095f..ca88c332 100644 --- a/MD applescript vs javascript/zipWithList.md +++ b/MD applescript vs javascript/zipWithList.md @@ -1,18 +1,3 @@ -```javascript -// zipWithList :: (a -> b -> c) -> [a] -> [b] -> [c] -const zipWithList = f => - // A list constructed by zipping with a - // custom function, rather than with the - // default tuple constructor. - xs => ys => ((xs_, ys_) => { - const lng = Math.min(length(xs_), length(ys_)); - return take(lng)(xs_).map( - (x, i) => f(x)(ys_[i]) - ); - })([...xs], [...ys]); -``` - - ```applescript -- zipWithList :: (a -> b -> c) -> [a] -> [b] -> [c] on zipWithList(f, xs, ys) @@ -29,4 +14,19 @@ on zipWithList(f, xs, ys) end tell end if end zipWithList +``` + + +```javascript +// zipWithList :: (a -> b -> c) -> [a] -> [b] -> [c] +const zipWithList = f => + // A list constructed by zipping with a + // custom function, rather than with the + // default tuple constructor. + xs => ys => ((xs_, ys_) => { + const lng = Math.min(length(xs_), length(ys_)); + return take(lng)(xs_).map( + (x, i) => f(x)(ys_[i]) + ); + })([...xs], [...ys]); ``` \ No newline at end of file diff --git a/MD applescript vs javascript/zipWithM.md b/MD applescript vs javascript/zipWithM.md index 731dfb1d..a8f403e6 100644 --- a/MD applescript vs javascript/zipWithM.md +++ b/MD applescript vs javascript/zipWithM.md @@ -1,3 +1,14 @@ +```applescript +-- zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c] +on zipWithM(fm, xs, ys) + -- A functor of the type to which fm lifts its result. + -- For example, Nothing/Left if any of the zip applications failed, + -- or Just/Right a list of the results, when all succeeded. + traverseList(my identity, zipWith(fm, xs, ys)) +end zipWithM +``` + + ```javascript // zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c] const zipWithM = f => @@ -7,15 +18,4 @@ const zipWithM = f => [...xs] )([...ys]) ); -``` - - -```applescript --- zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c] -on zipWithM(fm, xs, ys) - -- A functor of the type to which fm lifts its result. - -- For example, Nothing/Left if any of the zip applications failed, - -- or Just/Right a list of the results, when all succeeded. - traverseList(my identity, zipWith(fm, xs, ys)) -end zipWithM ``` \ No newline at end of file diff --git a/MD applescript vs javascript/zipWithN.md b/MD applescript vs javascript/zipWithN.md index 935e2303..b808a4ac 100644 --- a/MD applescript vs javascript/zipWithN.md +++ b/MD applescript vs javascript/zipWithN.md @@ -1,21 +1,3 @@ -```javascript -// zipWithN :: (a -> b -> ... -> c) -> ([a], [b] ...) -> [c] -function zipWithN() { - const - args = Array.from(arguments), - rows = args.slice(1).map(list), - f = compose(uncurryN(args[0]), TupleN), - n = Math.min(...rows.map(x => x.length)); - return 0 < n ? ( - take(n))(rows[0]).map( - (x, i) => f(rows.flatMap( - x => x[i] - )) - ) : []; -} -``` - - ```applescript -- zipWithN :: (a -> b -> ... -> c) -> ([a], [b] ...) -> [c] on zipWithN(f, rows) @@ -34,4 +16,22 @@ on zipWithN(f, rows) end script map(go, enumFromTo(1, minimum(map(my |length|, rows)))) end zipWithN +``` + + +```javascript +// zipWithN :: (a -> b -> ... -> c) -> ([a], [b] ...) -> [c] +function zipWithN() { + const + args = Array.from(arguments), + rows = args.slice(1).map(list), + f = compose(uncurryN(args[0]), TupleN), + n = Math.min(...rows.map(x => x.length)); + return 0 < n ? ( + take(n))(rows[0]).map( + (x, i) => f(rows.flatMap( + x => x[i] + )) + ) : []; +} ``` \ No newline at end of file diff --git a/asPrelude.applescript b/asPrelude.applescript index 2af9707e..56d15f45 100644 --- a/asPrelude.applescript +++ b/asPrelude.applescript @@ -6193,6 +6193,17 @@ on typeName(x) end if end typeName +-- unDigits :: [Int] -> Int +on unDigits(ds) + -- The integer with the given digits. + script go + on |λ|(a, x) + 10 * a + x + end |λ| + end script + foldl(go, 0, ds) +end unDigits + -- unQuoted :: String -> String on unQuoted(s) script p diff --git a/asPrelude.json b/asPrelude.json index b030281e..ed081218 100644 --- a/asPrelude.json +++ b/asPrelude.json @@ -2798,6 +2798,12 @@ "AS Prelude" ] }, + "unDigits": { + "code": "-- unDigits :: [Int] -> Int\non unDigits(ds)\n -- The integer with the given digits.\n script go\n on |λ|(a, x)\n 10 * a + x\n end |λ|\n end script\n foldl(go, 0, ds)\nend unDigits", + "tags": [ + "AS Prelude" + ] + }, "unQuoted": { "code": "-- unQuoted :: String -> String\non unQuoted(s)\n script p\n on |λ|(x)\n --{34, 39} contains id of x\n 34 = id of x\n end |λ|\n end script\n dropAround(p, s)\nend unQuoted", "tags": [ diff --git a/asPreludeMenu.json b/asPreludeMenu.json index b19b6c23..b96de915 100644 --- a/asPreludeMenu.json +++ b/asPreludeMenu.json @@ -461,6 +461,7 @@ "truncate": "-- truncate :: Num -> Int\non truncate(x)\n item 1 of properFraction(x)\nend truncate", "tupleFromList": "-- tupleFromList :: [a] -> (a, a ...)\non tupleFromList(xs)\n set lng to length of xs\n if 1 < lng then\n if 2 < lng then\n set strSuffix to lng as string\n else\n set strSuffix to \"\"\n end if\n script kv\n on |λ|(a, x, i)\n insertDict((i as string), x, a)\n end |λ|\n end script\n foldl(kv, {type:\"Tuple\" & strSuffix}, xs) & {length:lng}\n else\n missing value\n end if\nend tupleFromList", "typeName": "-- typeName :: a -> String\non typeName(x)\n set mb to lookupDict((class of x) as string, ¬\n {|list|:\"List\", |integer|:\"Int\", |real|:\"Float\", |text|:¬\n \"String\", |string|:\"String\", |record|:¬\n \"Record\", |boolean|:\"Bool\", |handler|:\"(a -> b)\", |script|:\"(a -> b\"})\n if Nothing of mb then\n \"Bottom\"\n else\n set k to Just of mb\n if k = \"Record\" then\n if keys(x) contains \"type\" then\n type of x\n else\n \"Dict\"\n end if\n else\n k\n end if\n end if\nend typeName", + "unDigits": "-- unDigits :: [Int] -> Int\non unDigits(ds)\n -- The integer with the given digits.\n script go\n on |λ|(a, x)\n 10 * a + x\n end |λ|\n end script\n foldl(go, 0, ds)\nend unDigits", "unQuoted": "-- unQuoted :: String -> String\non unQuoted(s)\n script p\n on |λ|(x)\n --{34, 39} contains id of x\n 34 = id of x\n end |λ|\n end script\n dropAround(p, s)\nend unQuoted", "uncons": "-- uncons :: [a] -> Maybe (a, [a])\non uncons(xs)\n set lng to |length|(xs)\n if 0 = lng then\n Nothing()\n else\n if (2 ^ 29 - 1) as integer > lng then\n if class of xs is string then\n set cs to text items of xs\n Just(Tuple(item 1 of cs, rest of cs))\n else\n Just(Tuple(item 1 of xs, rest of xs))\n end if\n else\n set nxt to take(1, xs)\n if {} is nxt then\n Nothing()\n else\n Just(Tuple(item 1 of nxt, xs))\n end if\n end if\n end if\nend uncons", "uncurry": "-- Given a curried/default function, returns an\n-- equivalent function on a tuple or list pair.\n-- A function over a pair, derived from\n-- a function over two arguments.\n-- uncurry :: (a -> b -> c) -> ((a, b) -> c)\non uncurry(f)\n if 1 < argvLength(f) then\n script\n on |λ|(ab)\n if class of ab is list then\n mReturn(f)'s |λ|(item 1 of ab, item 2 of ab)\n else\n mReturn(f)'s |λ|(|1| of ab, |2| of ab)\n end if\n end |λ|\n end script\n else\n script\n on |λ|(ab)\n if class of ab is list then\n f's |λ|(item 1 of ab)'s |λ|(item 2 of ab)\n else\n f's |λ|(|1| of ab)'s |λ|(|2| of ab)\n end if\n end |λ|\n end script\n end if\nend uncurry\n", diff --git a/listOfFunctionNames.txt b/listOfFunctionNames.txt index 1e13d96e..47a49f58 100644 --- a/listOfFunctionNames.txt +++ b/listOfFunctionNames.txt @@ -460,6 +460,7 @@ treeMenu :: Tree String -> IO [String] truncate :: Num -> Int tupleFromList :: [a] -> (a, a ...) typeName :: a -> String +unDigits :: [Int] -> Int unQuoted :: String -> String uncons :: [a] -> Maybe (a, [a]) uncurry :: (a -> b -> c) -> ((a, b) -> c)