Skip to content

Instantly share code, notes, and snippets.

@listrophy
Created December 8, 2016 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save listrophy/0df4903973a5381def871965267135c6 to your computer and use it in GitHub Desktop.
Save listrophy/0df4903973a5381def871965267135c6 to your computer and use it in GitHub Desktop.
Sendable Data for Elm
module SendableData exposing (..)
import Http
type SendableData e r wrapped
= NotSent wrapped
| Sending wrapped
| FailedSending e wrapped
| Sent r wrapped
type alias SendableWebData r wrapped
= SendableData Http.Error r wrapped
map : (wrapped -> wrapped_) -> SendableData e r wrapped -> SendableData e r wrapped_
map f data =
case data of
NotSent value ->
NotSent (f value)
Sending value ->
Sending (f value)
FailedSending e value ->
FailedSending e (f value)
Sent r value ->
Sent r (f value)
unwrap : SendableData e r wrapped -> wrapped
unwrap data =
case data of
NotSent value ->
value
Sending value ->
value
FailedSending e value ->
value
Sent r value ->
value
replace : SendableData e r wrapped -> wrapped_ -> SendableData e r wrapped_
replace orig newData =
case orig of
NotSent _ ->
NotSent newData
Sending _ ->
Sending newData
FailedSending error _ ->
FailedSending error newData
Sent resp _ ->
Sent resp newData
get : (wrapped -> b) -> SendableData e r wrapped -> b
get f data =
f (unwrap data)
fromResult : wrapped -> Result e r -> SendableData e r wrapped
fromResult wrapped result =
case result of
Err err ->
FailedSending err wrapped
Ok ok ->
Sent ok wrapped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment