Skip to content

Instantly share code, notes, and snippets.

@paulvictor
Created March 6, 2018 06:26
Show Gist options
  • Select an option

  • Save paulvictor/b3601fe773716266d51fc244a62f4646 to your computer and use it in GitHub Desktop.

Select an option

Save paulvictor/b3601fe773716266d51fc244a62f4646 to your computer and use it in GitHub Desktop.
FillInNestedRecords
module RecordMods where
import Prelude
import Control.Monad.Eff (Eff)
import Data.Foreign (Foreign)
import Data.Record (insert)
import Data.Symbol (SProxy(..))
newtype Person = Person {firstName :: String, lastName :: String, address :: Address}
newtype Address = Address {city :: String, state :: String, country :: String}
firstName = SProxy ∷ SProxy "firstName"
lastName = SProxy ∷ SProxy "lastName"
address = SProxy ∷ SProxy "address"
state = SProxy ∷ SProxy "state"
city = SProxy ∷ SProxy "city"
country = SProxy ∷ SProxy "country"
fillFirstAndLastName :: ∀ t22 t26. t22 → t26 → { lastName ∷ t26 , firstName ∷ t22 }
fillFirstAndLastName fn ln = (insert firstName fn >>> insert lastName ln) {}
fillState :: ∀ t5. t5 → { state ∷ t5 }
fillState s = (insert state s) {}
addCityAndCountry ∷ ∀ t52 t56. t52 → t56 → { state ∷ String } → { country ∷ t56 , city ∷ t52 , state ∷ String }
addCityAndCountry ci co = (insert city ci >>> insert country co)
addAddress ∷ Address → {firstName ∷ String, lastName ∷ String} → {firstName ∷ String, lastName ∷ String, address ∷ Address}
addAddress a = (insert address a)
getAString ∷ Eff _ String
getAString = pure "foo"
main :: ∀ t81. Eff t81 Person
main = do
ln ← getAString
s ← getAString
ci ← getAString
fn ← getAString
co ← getAString
let address = Address $ (insert city ci >>> insert country co) $ (insert state s) {}
pure $ Person $ addAddress address $ fillFirstAndLastName fn ln
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment