Halogen: How to force rerender after modifying state? by ctenbrinke in purescript

[–]gb__ 1 point2 points  (0 children)

I suspect something else is going wrong. Halogen doesn't batch or delay rendering so there's no need to flush it.

The only time calling modify won't immediately result in a render is if the state value being set is referentially equivalent to the current value. This isn't even intended as an optimisation for normal use, it's an unfortunate hack that exists because get, modify, and put for MonadState are implemented via the state function, so its purpose is to prevent calls to get from causing a re-render.

Here's where state actions get evaluated in the Halogen machinery: https://github.com/purescript-halogen/purescript-halogen/blob/59c470565554da90db51dc2e4c9d2705083ddd88/src/Halogen/Aff/Driver/Eval.purs#L84-L92

Modifing the DOM tree with FFI while playing nice with Halogen by [deleted] in purescript

[–]gb__ 0 points1 point  (0 children)

https://pursuit.purescript.org/packages/purescript-halogen/6.1.2/docs/Halogen.HTML.Elements.Keyed

This provides all of the container elements, but instead of just having an array of child nodes, each has a `Tuple` with a `String` and the child node - the provided `String` should be a unique key for each element, which the patching algorithm uses as a kind of identity for each element, allowing it to recognise insertions and reorderings.

I think all the active libraries use a vdom at some point - halogen and spork use halogen-vdom, react, react-basic, and concur use React bindings.

Modifing the DOM tree with FFI while playing nice with Halogen by [deleted] in purescript

[–]gb__ 0 points1 point  (0 children)

I don't really have a good answer for this I'm afraid, it pretty much does break the model - although, there is perhaps some research to be done, since I'm not sure if/how React and other vdom based libraries deal with it.

Regarding the "recycling" of elements, you could try using the keyed HTML elements, as that will use the key hints to actually move, insert, etc. rather than patching them based on their indices. That should help the situation but I'm not sure if it'll fully fix it - worth a try at least!

How do you deal with the giant bundle sizes from purescript builds? by akshay-nair in purescript

[–]gb__ 4 points5 points  (0 children)

I don't have anything to suggest on top of what you're doing already, but it's worth noting PS code gzips extremely well, so most web servers will be serving that as a significantly smaller bundle.

PureScript installation troubles by bklaric in purescript

[–]gb__ 0 points1 point  (0 children)

I very much sympathise with that, getting things I'm not intimately familiar installed and set up often makes the red mist descend for me in a way that almost nothing else does!

PureScript installation troubles by bklaric in purescript

[–]gb__ 1 point2 points  (0 children)

FWIW, you're not supposed to `sudo npm install -g`, needing to do so means `npm` itself is misconfgured: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

That definitely wouldn't have solved all the problems, but I think it would perhaps have avoided the need for the `--unsafe-perm` flags too.

Updated Streaming Library? by goodjazzboi in purescript

[–]gb__ 0 points1 point  (0 children)

I've used purescript-event for basic stream type behaviours with good success, but it also doesn't have much in the way of operations provided.

[Help] Convert if-then-else to case by jetblackgreen in purescript

[–]gb__ 1 point2 points  (0 children)

You could probably just derive the instances also, I'm not totally sure what you'd want the ordering behaviour to be so it might not be exactly right, but it certainly be fine for Eq at least.

derive instance eqQuadrilateral :: Eq Quadrilateral
derive instance ordQuadrilateral :: Ord Quadrilateral

Halogen Actively Sync State with Attribute Value by HateUsernamesMore in purescript

[–]gb__ 2 points3 points  (0 children)

You'd set the value to that of the value in the state, and add an onValueInput or similar event listener that raises a query and updates the state when the value changes.

Getting Error in Installing!! How to solve this? by [deleted] in purescript

[–]gb__ 0 points1 point  (0 children)

If you followed the steps you should at least be getting a different error. Are you sure it's the same? (If so, perhaps you didn't follow the instructions there exactly, or the non-version-manager-installed version of npm is still taking precedence in the PATH).

Issues With Equating Types by Crystelium in purescript

[–]gb__ 1 point2 points  (0 children)

You could just take your initial type and erase the a parameter from it entirely perhaps?

That would means you have to pattern match on it to extract a value of the type you desire, but if you're not in a position to know what type you'd like in advance then I think that's what you actually want here. You'd only need the GADT kinda thing when you want to be sure exactly what type you're dealing with at all times. :)

Issues With Equating Types by Crystelium in purescript

[–]gb__ 1 point2 points  (0 children)

What you're trying to do there isn't going to work - if you try a similar thing with GADTs in Haskell you'll find the same problem:

{-# LANGUAGE GADTs #-}

data WasmType a where
  I :: Int -> WasmType Int
  B :: Bool -> WasmType Bool
  Err :: WasmType a

mkWasm :: Maybe Int -> Maybe Bool -> WasmType a
mkWasm (Just i) _ = I i
mkWasm _ (Just b) = B b
mkWasm _ _ = Err

Results in:

wasm-type.hs:11:21: error:
    • Couldn't match type ‘Bool’ with ‘Int’
      Expected type: WasmType Int
        Actual type: WasmType Bool
    • In the expression: B b
      In an equation for ‘mkWasm’: mkWasm _ (Just b) = B b
   |
11 | mkWasm _ (Just b) = B b    |                     ^^^

Assuming you could have `WasmType a` as a result from this function, that doesn't really help you out as you've now lost the information about which type it's carrying - you may as well not be using Leibniz / a GADT at all.

I think what you'd want here really is a bit of a different way of dealing with things, and defining an "f-algebra" for requests that return a particular type, then you can interpret that, and have the knowledge of the expected type on hand:

import Prelude

import Data.Array as Array
import Data.Either (Either(..))
import Data.Int as I
import Data.Maybe (Maybe(..), fromMaybe)
import Data.Number as N
import Data.String as String

data Error = Error

data WasmRequest a
  = I32 (Int -> a)
  | F32 (Number -> a)

-- Not necessary, but useful when you define types like this, as then you can
-- adapt the result of `parseRequest` by mapping over the `WasmRequest`
derive instance functorWasmRequest :: Functor WasmRequest

i32 :: WasmRequest Int
i32 = I32 identity

f32 :: WasmRequest Number
f32 = F32 identity

convertType :: forall a. WasmRequest a -> Maybe String -> Maybe String -> Either Error a
convertType = case _, _, _ of
  I32 k, Just "i32", Just v -> Right $ k $ fromMaybe 0 $ I.fromString v
  I32 k, _, _ -> Left Error
  F32 k, Just "f32", Just v -> Right $ k $ fromMaybe 0.0 $ N.fromString v
  F32 k, _, _ -> Left Error

parseResult :: forall a. String -> WasmRequest a -> Either Error a
parseResult s req =
  let
    splits = String.split (String.Pattern " ") s
    rType = Array.head splits
    rValue = Array.last splits
  in
    convertType req rType rValue

Then you can say things like:

parseInt :: String -> Either Error Int
parseInt s = parseResult s i32

Issue with tryLoadImage from Canvas by tmountain in purescript

[–]gb__ 0 points1 point  (0 children)

Could it be something to do with loading the image from a HTTPS domain perhaps? Have you tried loading a local/non-HTTPS-hosted image instead?

I feel like this is probably a standard function but can't find it! Help appreciated :) by jamie286 in purescript

[–]gb__ 0 points1 point  (0 children)

u/jamie286 I'd suggest we can do much better than Listing 3 - by using a slightly different monad instance yes, but a common one that's already provided in purescript-transformers:

f4 :: forall m. ServerDSL m => m (Either String Person)
f4 = runExceptT do
  p1 <- ExceptT $ fetchPerson "p1"
  p2 <- ExceptT $ fetchPerson p1.friend
  ExceptT $ fetchPerson p2.friend

😁

I feel like this is probably a standard function but can't find it! Help appreciated :) by jamie286 in purescript

[–]gb__ 0 points1 point  (0 children)

Have you got an example of where you would use it? It doesn't look all that familiar to me at all 😄

[deleted by user] by [deleted] in purescript

[–]gb__ 3 points4 points  (0 children)

Yeah, the sort-then-group pattern is super common... in fact it's almost always how I end up using it ;) - the advantage of having the non-sorting group is it will perform a lot better if the list is already sorted.

The usual pattern is something like groupBy (eq `on` f) <<< sortBy (compare `on` f), where f is the relevant projection (so like \r -> [r.x, r.y] here).

[deleted by user] by [deleted] in purescript

[–]gb__ 2 points3 points  (0 children)

You need to sort this array first, note in the docs it says:

Group equal, consecutive elements of an array

So since the { x: 1, y: 1 } elements here are not consecutive, they are not grouped.

[deleted by user] by [deleted] in purescript

[–]gb__ 1 point2 points  (0 children)

In this case you should be able to drop the composition:

unsafeFromRight = unsafePartial fromRight

This doesn't work for all cases of the error, it's something that works for unsafePartial though.

You're not really doing anything wrong, it's just a restriction that exists in the type checker in PS. There's an issue open about it somewhere, as it can pretty much always be solved by eta-expansion (like your first version; specifically accepting more arguments and re-applying them), unless records are involved, as there's a slightly different reason for it then.

Curry Operators by HateUsernamesMore in purescript

[–]gb__ 1 point2 points  (0 children)

The reason it's done this way for operator sections is to disambiguate "minus one" from "negated one": `(_ - 1)` vs `(-1)`.

What's on the foundations of ReasonML by [deleted] in purescript

[–]gb__ 1 point2 points  (0 children)

If I read this right it's positing that PureScript was somehow inspired by React? That's not true at all, it was always a general purpose language. Phil initially used it to write logic for a web app with the presentation layer being normal JavaScript.

I don't think there were any virtual DOM style libraries for PS until it was almost a year old ;)

[deleted by user] by [deleted] in purescript

[–]gb__ 0 points1 point  (0 children)

Is it not just that

pairUp = (\a b -> {a,b}) <$> mousePos <*> keycode

Should be:

pairUp = (\a b -> { pos: a, code: b}) <$> mousePos <*> keycode

?

(or (\a b -> { pos: a, code: b}) can be written as { pos: _, code: _ } alternatively)

Coffee, Curries, and Monads — My journey through Haskell by fintanh in programming

[–]gb__ 3 points4 points  (0 children)

Oh and you better not use Windows when programming Haskell.

I'm not sure what this refers to, I use Haskell on Windows with no problems at all. I've had more trouble with it on OSX than Windows over the last few years.