Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
|
|
@ -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 #
|
||||
|
||||
|
|
|
|||
3
Task/Monads-Maybe-monad/J/monads-maybe-monad-1.j
Normal file
3
Task/Monads-Maybe-monad/J/monads-maybe-monad-1.j
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
'J N'=. s:'Just';'Nothing'
|
||||
unit =. J,&<]
|
||||
bind =. {{u@(1{::])^:(N-.@-:])}}
|
||||
3
Task/Monads-Maybe-monad/J/monads-maybe-monad-2.j
Normal file
3
Task/Monads-Maybe-monad/J/monads-maybe-monad-2.j
Normal 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
|
||||
14
Task/Monads-Maybe-monad/J/monads-maybe-monad-3.j
Normal file
14
Task/Monads-Maybe-monad/J/monads-maybe-monad-3.j
Normal 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
|
||||
|
|
@ -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 _
|
||||
┌──┐
|
||||
│_.│
|
||||
└──┘
|
||||
3
Task/Monads-Maybe-monad/K/monads-maybe-monad.k
Normal file
3
Task/Monads-Maybe-monad/K/monads-maybe-monad.k
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
unit:(::)
|
||||
bind:{[k;a]:[a~`N;a;k a]}
|
||||
kcomp:{[f;g](bind[g;]@f@)} / (>=>) i.e. kleisli composition
|
||||
17
Task/Monads-Maybe-monad/V-(Vlang)/monads-maybe-monad.v
Normal file
17
Task/Monads-Maybe-monad/V-(Vlang)/monads-maybe-monad.v
Normal 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue