purescript

How to use type constrains with Exists

。_饼干妹妹 提交于 2019-12-23 03:51:08
问题 data Foo a = Foo a I can create an array of Exists https://github.com/purescript/purescript-exists [(mkExists (Foo 0)), (mkExists (Foo "x"))] How can I use type classes? I want to get ["0", "x"] getStrings :: Array (Exists Foo) -> Array String getStrings list = map (runExists get) list where get :: forall a. Show a => Foo a -> String get (Foo a) = show a No type class instance was found for Prelude.Show _0 The instance head contains unknown type variables. Consider adding a type annotation.

How to use type constrains with Exists

本秂侑毒 提交于 2019-12-23 03:51:02
问题 data Foo a = Foo a I can create an array of Exists https://github.com/purescript/purescript-exists [(mkExists (Foo 0)), (mkExists (Foo "x"))] How can I use type classes? I want to get ["0", "x"] getStrings :: Array (Exists Foo) -> Array String getStrings list = map (runExists get) list where get :: forall a. Show a => Foo a -> String get (Foo a) = show a No type class instance was found for Prelude.Show _0 The instance head contains unknown type variables. Consider adding a type annotation.

Purescript types for buildQueryString function

会有一股神秘感。 提交于 2019-12-22 17:37:13
问题 I am new to Purescript and I am trying to write a function that can take any record value and iterate over the fields and values and build a querystring. I am thinking something like: buildQueryString :: forall a. PropertyTraversible r => r -> String which I want to use like this: buildQueryString {name: "joe", age: 10} -- returns: "name=joe&age=10" Is there a way to write something like that in Purescript with existing idioms or do I have to create my own custom Type Class for this? 回答1:

Purescript thermite and websockets

二次信任 提交于 2019-12-14 03:51:40
问题 I am trying to use purescript-thermite to build an application using websockets. The idea is that the application connects to some server using websockets and live-updates the HTML page. However, I cannot find a way how to wire it into the thermite workflow. I have a spec that is made up of render and performAction . The render has access to the dispatch function. However, I need to start the websockets before rendering the element (I could probably put it e.g. into main ), but upon arrival

Write multi-line statement on single line

跟風遠走 提交于 2019-12-12 13:09:10
问题 In Haskell I can write a multi-line statement on a single line, like do {x<-[1,2];y<-[3,4];return (x,y)} but in Purescript even a single-statement do-statement with curly braces will not compile. Is there different syntax to achieve this? 回答1: No, PureScript has no syntax for doing this kind of thing. Aside from just not using do ;) [1, 2] >>= \x -> [3, 4] >>= \y -> pure (Tuple x y) 来源: https://stackoverflow.com/questions/46461251/write-multi-line-statement-on-single-line

How do I convert a list of chars to a string in purescript

♀尐吖头ヾ 提交于 2019-12-12 11:25:00
问题 I'm looking for an idiomatic way to write a function List Char -> String in Purescript. This seems like a simple thing to do, but I'm new to Purescript and have been browsing documentation for a while now with no progress! Background information: I am porting a simple function from Haskell to Purescript generateId :: Int -> [Char] This generates a String of specified length. It was quite easy to convert the code to use List Char operations (where List is from Data.List in Purescript). In

Why don't these types unify when using type constraints and a typeclass instance?

吃可爱长大的小学妹 提交于 2019-12-12 05:16:02
问题 So, I am attempting to develop a function that expects a polymorphic function. I start with and create a class Foo , to represent the constrained type argument in the example function. module Test where class Foo a Along with a random type for this example... newtype Bar = Bar Int In order to pass Bar to anything that has a Foo constraint, it needs to have a typeclass, so I do so: instance fooBar :: Foo Bar Everything should be perfectly fine, however, this doesn't work, psc doesn't think the

Purescript error - can't match equivalent types?

让人想犯罪 __ 提交于 2019-12-12 02:37:26
问题 Error found: in module Test at /Users/arahael/dev/test/test.purs line 14, column 37 - line 14, column 59 Could not match constrained type (Spreadable a0) => Array a0 -> Int with type (Spreadable a1) => Array a1 -> Int while trying to match type (Spreadable a0) => Array a0 -> Int with type (Spreadable a1) => Array a1 -> Int while checking that expression mkFn2 get_biscuits has type Fn2 String (forall a. (Spreadable a) => Array a -> Int) Int in value declaration packetMaker where a1 is a rigid

How can I create an array with polymorphic data?

南楼画角 提交于 2019-12-12 01:16:08
问题 I am trying to do this data Foo a = Foo a data FooWrapper = FooWrapper (forall a. Foo a) foo = [FooWrapper (Foo 0), FooWrapper (Foo "")] But there is an error Could not match type Int with type a0 回答1: Existential types don't quite work the same in PureScript as they do in Haskell, so usually we use the purescript-exists library for this kind of thing. The equivalent using Exists would be: import Data.Exists (Exists(), mkExists) data Foo a = Foo a data FooWrapper = FooWrapper (Exists Foo) foo

Purescript Halogen, side effect (random number)

ぃ、小莉子 提交于 2019-12-11 04:46:15
问题 In a PureScript Halogen project, I would like to set the state to a random number, but how do I extract the value? The normal r <- randomInt 1 10 does not compile when it's inside the eval function. module Main where import Prelude import Control.Monad.Eff (Eff) import Control.Monad.Eff.Random (randomInt, RANDOM) import Halogen as H import Halogen.HTML.Events.Indexed as HE import Halogen.HTML.Indexed as HH import Halogen.Util (runHalogenAff, awaitBody) type State = { n::Int } initialState ::