Skip to content

Instantly share code, notes, and snippets.

@akbashev
Last active October 20, 2022 11:39
Show Gist options
  • Save akbashev/f2716ce4e51362d0dc4e9d0433d84983 to your computer and use it in GitHub Desktop.
Save akbashev/f2716ce4e51362d0dc4e9d0433d84983 to your computer and use it in GitHub Desktop.
Playground
module Main where
combine :: Int -> Int -> Int
combine a b = a + b
increase = combine 1
decrease = combine (-1)
data Optional a =
Some a | None
instance Functor Optional where
fmap _ None = None
fmap f (Some a) = Some (f a)
deriving instance Show a => Show (Optional a)
data State = State {
value :: Optional Int
}
data Action =
Increase | Decrease | Combine Int
reduce :: Action -> State -> State
reduce Increase s = State { value = fmap increase (value s) }
reduce Decrease s = State { value = fmap decrease (value s) }
reduce (Combine number) s = State { value = fmap (combine number) (value s) }
main :: IO ()
main = do
putStrLn "Hello, Haskell!"
putStrLn $ show $ increase $ decrease $ increase $ decrease 1
putStrLn $ show (combine 2 3)
let state = reduce(Increase) $ reduce(Decrease) $ reduce(Increase) $ reduce(Decrease)(State { value = Some 1 })
putStrLn $ show $ value state
val combine: (Int) -> (Int) -> (Int) = { a ->
{ b ->
a + b
}
}
val increase: (Int) -> Int = combine(1)
val decrease: (Int) -> Int = combine(-1)
infix fun <T> T.then(func: (T) -> T): T = func(this)
fun Int.combine(with: Int): Int = this + with
fun Int.increase(): Int = combine(1)
fun Int.decrease(): Int = combine(-1)
sealed class Optional<out A> {
data class Some<out A>(val value: A): Optional<A>()
object None: Optional<Nothing>()
companion object {
fun <A>from(value: A?): Optional<A> {
return value?.let { Optional.Some(value) } ?: Optional.None
}
}
}
fun <T> Optional<T>.value() = when (this) {
is Optional.Some -> value
is Optional.None -> null
}
fun <T> Optional<T>.map(f: (T) -> T) = when (this) {
is Optional.Some -> Optional.Some(f(value))
is Optional.None -> Optional.None
}
fun <T, V> Optional<T>.flatMap(f: (T) -> Optional<V>) = when (this) {
is Optional.Some -> f(value)
is Optional.None -> Optional.None
}
data class State<V>(val value: Optional<V>)
sealed class Action {
class increase: Action()
class decrease: Action()
data class combine(val number: Int): Action()
}
val reduce: (Action) -> (State<Int>) -> (State<Int>) = { action ->
{ state ->
when (action) {
is Action.increase -> State(state.value.map(increase))
is Action.decrease -> State(state.value.map(decrease))
is Action.combine -> State(state.value.map(combine(action.number)))
}
}
}
fun main() {
println("Hello Kotlin/Native!")
println(increase(decrease(increase(decrease(1)))))
println(1 then decrease then increase then decrease then increase)
println(
1
.decrease()
.increase()
.decrease()
.increase()
)
println(combine(2)(3))
println(2.combine(3))
val state = State<Int>(Optional.Some(1))
.then(reduce(Action.decrease()))
.then(reduce(Action.increase()))
.then(reduce(Action.decrease()))
.then(reduce(Action.increase()))
println(state.value.value())
}
let combine: (Int) -> (Int) -> (Int) = { a in
{ b in
a + b
}
}
let increase: (Int) -> Int = combine(1)
let decrease: (Int) -> Int = combine(-1)
precedencegroup ForwardApplication {
associativity: left
}
infix operator |> : ForwardApplication
func |> <A, B>(a: A, f: (A) -> B) -> B {
return f(a)
}
extension Int {
func combine(with value: Int) -> Int {
self + value
}
func increase() -> Int { self.combine(with: 1) }
func decrease() -> Int { self.combine(with: 1) }
}
struct State<V> { let value: Optional<V> }
enum Action {
case increase
case decrease
case combine(Int)
}
let reduce: (Action) -> (State<Int>) -> (State<Int>) = { action in
{ state in
switch action {
case .increase: return State(value: state.value.map(increase))
case .decrease: return State(value: state.value.map(decrease))
case .combine(let number): return State(value: state.value.map(combine(number)))
}
}
}
print("Hello Swift")
print(increase(decrease(increase(decrease(1)))))
print(1 |> decrease |> increase |> decrease |> increase)
print(
1
.decrease()
.increase()
.decrease()
.increase()
)
print(combine(2)(3))
print(2.combine(with: 3))
let state = State<Int>(value: Optional.some(1))
|> reduce(.decrease)
|> reduce(.increase)
|> reduce(.decrease)
|> reduce(.increase)
print(state.value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment