diff options
Diffstat (limited to 'std')
-rw-r--r-- | std/List.bruijn | 5 | ||||
-rw-r--r-- | std/Math.bruijn | 9 | ||||
-rw-r--r-- | std/Monad.bruijn | 3 | ||||
-rw-r--r-- | std/Number.bruijn | 4 | ||||
-rw-r--r-- | std/Number/Ternary.bruijn | 3 | ||||
-rw-r--r-- | std/String.bruijn | 6 |
6 files changed, 26 insertions, 4 deletions
diff --git a/std/List.bruijn b/std/List.bruijn index 488c131..ed16530 100644 --- a/std/List.bruijn +++ b/std/List.bruijn @@ -510,6 +510,11 @@ iterate z [[[rec]]] ⧗ (a → a) → a → (List a) :test (take (+5) (iterate i (+4))) (take (+5) (repeat (+4))) :test (take (+0) (iterate ++‣ (+0))) (empty) +# enumerate list +enumerate zip (iterate ++‣ (+0)) ⧗ (List a) → (List (Pair Number a)) + +:test (enumerate "abc") (((+0) : 'a') : (((+1) : 'b') : {}((+2) : 'c'))) + # calculates all fixed points of given function as a list y* [[[0 1] <$> 0] xs] ⧗ (List a) → (List b) xs [[1 <! ([[1 2 0]] <$> 0)]] <$> 0 diff --git a/std/Math.bruijn b/std/Math.bruijn index e2a9466..82a787d 100644 --- a/std/Math.bruijn +++ b/std/Math.bruijn @@ -213,11 +213,18 @@ characteristic-primes ki : (ki : (sieve s0)) ⧗ (List Bool) s0 [[[ki : (0 2)]]] # prime number sequence -primes L.map fst (L.filter snd (L.zip (L.iterate ++‣ (+0)) characteristic-primes)) ⧗ (List Number) +primes L.map fst (L.filter snd (enumerate characteristic-primes)) ⧗ (List Number) # slower but cooler prime number sequence primes* L.nub ((…≠?… (+1)) ∘∘ gcd) (L.iterate ++‣ (+2)) ⧗ (List Number) +# prime factors +factors \divs primes ⧗ Number → (List Number) + divs y [[&[[&[[3 ⋅ 3 >? 4 case-1 (=?0 case-2 case-3)]] (quot-rem 2 1)]]]] + case-1 4 >? (+1) {}4 empty + case-2 3 : (5 1 (3 : 2)) + case-3 5 4 2 + # π as a list of decimal digits # translation of unbounded spigot algorithm by Jeremy Gibbons # TODO: faster! diff --git a/std/Monad.bruijn b/std/Monad.bruijn index 97291eb..0dae29e 100644 --- a/std/Monad.bruijn +++ b/std/Monad.bruijn @@ -37,6 +37,9 @@ sequence foldr (lift-m2 cons) {}empty ⧗ (List (M a)) → (M (List a)) >‣ [sequence ∘∘ 0] +# traverses list based on modifier +traverse sequence ∘∘ map ⧗ (a → (M b)) → (N a) → (M (N b)) + # performs action n times replicate-m >replicate ⧗ Number → (M a) → (M (List a)) diff --git a/std/Number.bruijn b/std/Number.bruijn index b2f3631..25a1f6c 100644 --- a/std/Number.bruijn +++ b/std/Number.bruijn @@ -9,9 +9,9 @@ # the following functions are only here because of recursive imports of list/ternary # converts number to list of its digits -number→list [=?0 {}(+0) (z [[rec]] 0)] ⧗ Number → (List Number) +number→list [=?0 {}(+0) <~>(z [[[rec]]] empty 0)] ⧗ Number → (List Number) rec =?0 case-end case-rec - case-rec (1 (0 / (+10))) ; (0 % (+10)) + case-rec &[[4 (0 : 3) 1]] (quot-rem 0 (+10)) case-end empty :test (number→list (+0)) ({}(+0)) diff --git a/std/Number/Ternary.bruijn b/std/Number/Ternary.bruijn index 5e93c66..3695740 100644 --- a/std/Number/Ternary.bruijn +++ b/std/Number/Ternary.bruijn @@ -500,10 +500,11 @@ double-shift [[[[[left : right]] (force 2 1) (force 2 0)]]] left force 4 ((mst* 0) ↑ (+0) + (↑⁰1)) right force 4 ↑⁰0 -# efficient quotient/remainder implementation for balanced ternary +# "efficient" quotient/remainder implementation for balanced ternary # technique by Douglas W. Jones # algorithm originally intended for fixed-width numbers (=> ugly hacks with force+log3) # TODO: remove the final `huh` correction step (probably some off-by-one bug?) +# TODO: not actually that efficient right now quot-rem [[[[[z [[[[rec]]]] 1 (+0) 4]]] <?0 (max (log3 1) (log3 0)) 0]] ⧗ Number → Number → (Pair Number Number) rec =?2 huh (double-shift 5 1 0 [[compare-case eq gt lt 1 (+0)]]) huh (>?1 ⋀? 6) ⋁? (<?1 ⋀? \6) (--0 : (1 + 7)) (0 : 1) diff --git a/std/String.bruijn b/std/String.bruijn index e1f4f4b..6ffd1f4 100644 --- a/std/String.bruijn +++ b/std/String.bruijn @@ -110,6 +110,12 @@ number→string (map C.number→char) ∘ number→list ⧗ Number → String :test (number→string (+123)) ("123") +# trims whitespace from the beginning and end of a string +trim w b (reverse ∘ (drop-while (ni? "\t\r\n "))) ⧗ String → String + +:test (trim " ab ") ("ab") +:test (trim "\t\r\nab\t\r\n") ("ab") + # splits string by newline character lines z [[rec]] ⧗ String → (List String) rec build (break (B.eq? '\n') 0) |