Data update

This commit is contained in:
Ingy döt Net 2024-10-16 18:07:41 -07:00
parent 81fd053722
commit 52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions

View file

@ -79,9 +79,9 @@ COMMENT
# Using the bind function, we can nest applications of safe functions, #
# without the compiler choking on unexpectedly wrapped values returned from #
# other functions of the same kind. #
REAL root one over four = value OF ( MAYBE( TRUE, 4 ) BIND safe reciprocal BIND safe root );
# other functions of the same kind. E.g.: #
# REAL root one over four #
# = value OF ( MAYBE( TRUE, 4 ) BIND safe reciprocal BIND safe root ); #
# print( ( root one over four, newline ) ); #
# -> 0.5 #

View file

@ -0,0 +1,3 @@
'J N'=. s:'Just';'Nothing'
unit =. J,&<]
bind =. {{u@(1{::])^:(N-.@-:])}}

View file

@ -0,0 +1,3 @@
fail =. {{unit@:u`(N"_)@.v}} NB. if v —> return Nothing, else apply u
show =. [: ": (,' '&,)&":&>/
comp =. [: > {{<x`:6 bind>y}}/@(,<@unit) NB. multi-compose

View file

@ -0,0 +1,14 @@
^.%:_2 NB. ln ∘ sqrt; gives complex numbers for inputs < 0
0.346574j1.5708
sqrt=. %: fail ([: +./ <&0) NB. return Nothing where result would've been complex
ln =. ^. fail ([: +./ <:&0) NB. same
show ln bind sqrt bind unit 2 3 4
`Just 0.346574 0.549306 0.693147
show ln bind sqrt bind unit 2 _3 4 NB. short-circuits on input < 0
`Nothing
show@(ln bind)@(sqrt bind)@unit&> 2 _3 4
`Just 0.346574
`Nothing
`Just 0.693147
show unit@:*:`ln`sqrt comp 2 3 4
`Just 0.120113 0.301737 0.480453

View file

@ -1,27 +0,0 @@
NB. monad implementation:
unit=: <
bind=: (@>)( :: ])
NB. monad utility
safeVersion=: (<@) ( ::((<_.)"_))
safeCompose=:dyad define
dyad def 'x`:6 bind y'/x,unit y
)
NB. unsafe functions (fail with infinite arguments)
subtractFromSelf=: -~
divideBySelf=: %~
NB. wrapped functions:
safeSubtractFromSelf=: subtractFromSelf safeVersion
safeDivideBySelf=: divideBySelf safeVersion
NB. task example:
safeSubtractFromSelf bind safeDivideBySelf 1
┌─┐
│0│
└─┘
safeSubtractFromSelf bind safeDivideBySelf _
┌──┐
│_.│
└──┘

View file

@ -0,0 +1,3 @@
unit:(::)
bind:{[k;a]:[a~`N;a;k a]}
kcomp:{[f;g](bind[g;]@f@)} / (>=>) i.e. kleisli composition

View file

@ -0,0 +1,17 @@
type Maybe = f64 | string
fn (m Maybe) div(x f64, y f64) Maybe {
mut result := m{}
result = x / y
if result.str().contains_any_substr(["inf","nan"]) {result = "none"}
return result
}
fn main() {
mb := Maybe{}
for a in [15, 0, 5] {
for b in [5, 0, 0] {
println(mb.div(a, b))
}
}
}