YAPC::EU 2018 Glasgow Update!
This commit is contained in:
parent
22f33d4004
commit
4e2d22a71d
1170 changed files with 15042 additions and 3047 deletions
|
|
@ -0,0 +1,3 @@
|
|||
USE: sequences.extras
|
||||
: strip-comments ( str -- str' )
|
||||
[ "#;" member? not ] take-while "" like ;
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
\ Rosetta Code Strip Comment
|
||||
|
||||
: LASTCHAR ( addr len -- addr len c) 2DUP + 1- C@ ;
|
||||
|
||||
: COMMENT? ( char -- ? ) S" #;" ROT SCAN NIP ; \ is char '#' or ';'
|
||||
|
||||
: -COMMENT ( addr len -- addr len') \ removes # or ; comments
|
||||
BEGIN
|
||||
LASTCHAR COMMENT? 0=
|
||||
WHILE \ while not a comment char...
|
||||
1- \ reduce length by 1
|
||||
REPEAT
|
||||
1- ; \ remove 1 more (the comment char)
|
||||
|
||||
\ -TRAILING is resident in desktop Forth systems like Swift Forth
|
||||
\ shown here for demonstration
|
||||
: -TRAILING ( adr len -- adr len') \ remove trailing spaces
|
||||
BEGIN
|
||||
LASTCHAR BL =
|
||||
WHILE \ while lastchar = blank char
|
||||
1- \ reduce length by 1
|
||||
REPEAT ;
|
||||
|
||||
: COMMENT-STRIP ( addr len -- addr 'len) -COMMENT -TRAILING ;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
S" apples, pears # and bananas" COMMENT-STRIP TYPE apples, pears ok
|
||||
S" apples, pears ; and bananas" COMMENT-STRIP TYPE apples, pears ok
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
const main = () => {
|
||||
|
||||
const src = `apples, pears # and bananas
|
||||
apples, pears ; and bananas`;
|
||||
|
||||
return unlines(
|
||||
map(preComment(chars(';#')),
|
||||
lines(src)
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
// preComment :: [Char] -> String -> String
|
||||
const preComment = cs => s =>
|
||||
strip(
|
||||
takeWhile(
|
||||
curry(flip(notElem))(cs),
|
||||
s
|
||||
)
|
||||
);
|
||||
|
||||
// GENERIC FUNCTIONS ------------------------------
|
||||
|
||||
// chars :: String -> [Char]
|
||||
const chars = s => s.split('');
|
||||
|
||||
// curry :: ((a, b) -> c) -> a -> b -> c
|
||||
const curry = f => a => b => f(a, b);
|
||||
|
||||
// flip :: (a -> b -> c) -> b -> a -> c
|
||||
const flip = f => (a, b) => f.apply(null, [b, a]);
|
||||
|
||||
// lines :: String -> [String]
|
||||
const lines = s => s.split(/[\r\n]/);
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f);
|
||||
|
||||
// notElem :: Eq a => a -> [a] -> Bool
|
||||
const notElem = (x, xs) => -1 === xs.indexOf(x);
|
||||
|
||||
// strip :: String -> String
|
||||
const strip = s => s.trim();
|
||||
|
||||
// takeWhile :: (a -> Bool) -> [a] -> [a]
|
||||
// takeWhile :: (Char -> Bool) -> String -> String
|
||||
const takeWhile = (p, xs) => {
|
||||
let i = 0;
|
||||
const lng = xs.length;
|
||||
while ((i < lng) && p(xs[i]))(i = i + 1);
|
||||
return xs.slice(0, i);
|
||||
};
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue