Skip to content

Instantly share code, notes, and snippets.

@jinjor
Last active September 16, 2015 08:42
Show Gist options
  • Select an option

  • Save jinjor/59b2cf60677e68e6550d to your computer and use it in GitHub Desktop.

Select an option

Save jinjor/59b2cf60677e68e6550d to your computer and use it in GitHub Desktop.
elm-effects-util
module Lib.EffectsUtil where
import Task exposing (..)
import Effects exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Time exposing (..)
import Native.EffectsUtil
mapResult : (Result x a -> b) -> Task x a -> Task y b
mapResult f task = Task.map f (Task.toResult task)
toEffects' : (Result x a -> b) -> Task x a -> Effects b
toEffects' f task = Effects.task <| mapResult f task
toEffects : (x -> b) -> (a -> b) -> Task x a -> Effects b
toEffects f g task = toEffects' (\r -> case r of
Ok x -> g x
Err a -> f a
) task
anyway : c -> Task a b -> Effects c
anyway action task = toEffects' (always action) task
effectsOf : a -> Effects a
effectsOf = Effects.task << Task.succeed
type Either a b = Left a | Right b
try' : (ea -> ec) -> (eb -> ec) -> Task ea a -> (a -> Task eb b) -> Task ec b
try' a b task f = mapError a task `andThen` (\a -> mapError b (f a))
try : Task ea a -> (a -> Task eb b) -> Task (Either ea eb) b
try = try' Left Right
catch : Task x a -> (x -> a) -> Task y a
catch task f = task `onError` (Task.succeed << f)
map' : Task x a -> (a -> b) -> Task x b
map' task f = Task.map f task
getCurrentTime : Task x Time
getCurrentTime = Native.EffectsUtil.getCurrentTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment