Skip to content

Instantly share code, notes, and snippets.

@robkuz
robkuz / SassMeister-input.scss
Created April 16, 2015 11:17
Generated by SassMeister.com.
// ----
// Sass (v3.4.12)
// Compass (v1.0.3)
// ----
$default-font-size: 14px;
$hc-yellow: yellow;
$hc-darkblue: blue;
$hc-darkyellow: darkyellow;
$hc-blue: blue;
@robkuz
robkuz / normalizer.hs
Created February 23, 2016 10:42
How to extract a nested unbalanced structure
-- I was thinking on how to normalize a structure like
-- (Either String (Either String Int)) into
-- Either String Int
-- my first attempt was to pattern match
normalizer :: (Either String (Either String Int)) -> Either String Int
normalizer n = do
case n of
Left l -> Left l
Right r -> r
@robkuz
robkuz / KeyValueMap.purs
Last active August 11, 2022 16:50
Some thoughts on Purescript Tuples, Records and Dict
--The only way to create what other languages call a **Dictionary** Is to create a `Array (Tuple String a)`.
--This is excessively lengthy (imo). Specifically as the change to 0.7 also removed the Haskell Tuple syntax.
--example
d = [Tuple "x1" 1, Tuple "x2" 2, Tuple "y1" 3, Tuple "y2" 4]
-- prior t0 0.7 you could at least do it this way
d = [("x1", 1), ("x2", 2), ("y1", 3), ("y2", 4)]
-- the problem with the above however is that it is possible to assign the same "label" multiple times.
@robkuz
robkuz / Aff.md
Last active April 25, 2016 15:41
Fun with Aff

I understand what get1 does and that is fine

get1 = attempt $ Affjax.get "http://www.google.com"

I think I know what liftAff does BUT the compiler knows better a

get2 = liftAff $ attempt $ Affjax.get "http://www.google.com"

and gives the following the error message

@robkuz
robkuz / main.md
Created April 26, 2016 11:29
Using a synonym for a Monad

Id like to make the signature of foo a bit better readable

foo :: forall e m. (MonadAff (ajax :: AJAX | e) m) => m (Either Error (AffjaxResponse Json))

What I'd like to do is

type AjaxAff = MonadAff (ajax :: AJAX | e)
foo :: forall m. (AjaxAff m) => m (Either Error (AffjaxResponse Json))

but this gives me

@robkuz
robkuz / typesig.md
Created April 26, 2016 15:41
Why need the full blown type signature?

I have the following value and its signature

type JsonResponse = Affjax.AffjaxResponse Json
getUser :: forall e m. (MonadAff (ajax :: Affjax.AJAX | e) m) => String -> m (Either Error JsonResponse)
getUser user = liftAff $ attempt $ Affjax.get $ "http://someservice.com?user=" ++ user

Now I want to use that in combination with a Maybe. Easy enough just use map (or the alias operator)

applyUser' = getUser <$> Just "Bob"
@robkuz
robkuz / Main.js
Last active May 6, 2016 13:29
Access Global Object from Purescript
"use strict";
// module Main
exports.instGlobal = function(name){
return function (value) {
global[name]=value;
return global[name];
}
}
@robkuz
robkuz / api_design.md
Last active May 12, 2016 11:30
API design howto

I am wrapping some matrix library from javascript into Purescript. And I am thinking which signatures my two creation functions for a matrix should have. Their main difference is that in one case you just give a Vector (JS Array) and in the other case you give a 2D Array. In the first case I can easily construct my Matrix. But in the second case I have a error condition to check as the array elemements of of the 2D array (themselves being arrays) all need to be of the same lenght.

data Matrix = Matrix (Array (Array Number))
data Err = Err -- just some error, really not import what kind

make :: (Array (Array Number)) -> Either Err Matrix

@robkuz
robkuz / error-handling-refactoring.purs
Created May 25, 2016 12:12
refactoring some convoluted error handling code
initial code with some really convoluted error handling.
The problem on this is that it must check for 2 different error cases and also return a different result in those error cases
dot :: Matrix -> Matrix -> Either MatrixError Number
dot (Matrix a sa) (Matrix b sb) = if areSimilarVectors sa sb then Right $ dot' a b else failed sa sb
where
areSimilarVectors s1 s2 = isVector s1 s2 && isSameSize s1 s2
dot' a b = _dot (join a) (join b)
isVector sa sb = fst sa == 1 && fst sb == 1
isSameSize sa sb = snd sa == snd sb
@robkuz
robkuz / polymorphic.md
Last active June 16, 2016 15:24
Emulating Haskell Type classes in F# and creating a bind (>>=) function

Here is someway to support method overloading with F# and to create a bind function.

first create an inline operator

    let inline (>>=) (f: ^U -> ^T) (t:^T)  = 
        let bind' = (^T : (member bind : (^U -> ^T) -> ^T) (t, f))
        bind'