Skip to content

Instantly share code, notes, and snippets.

View danidiaz's full-sized avatar

Daniel Díaz Carrete danidiaz

View GitHub Profile
@etorreborre
etorreborre / registry.hs
Last active October 9, 2021 09:10
Dependency injection with registry
{-
See https://www.reddit.com/r/haskell/comments/q1oyws/dependency_injection_using_a_recordoffunctions
for full context
-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE DeriveGeneric #-}

title: Hyperfunctions, Breadth-First Traversals, and Search author: Oisín Kidney patat: theme: codeBlock: [vividBlack] code: [vividBlack] incrementalLists: true eval: ruby:

liftType :: forall t. Typeable t => Q Type
liftType = do
let rep = typeRep @t
go rep
where
go :: forall (a :: k). TypeRep a -> Q Type
go tr =
case tr of
Con tyCon -> do
let
@sjoerdvisscher
sjoerdvisscher / LinearStateMonad.hs
Last active August 12, 2025 12:56
Alternative ways to write the linear quicksort from https://www.tweag.io/blog/2021-02-10-linear-base/
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE QualifiedDo #-}
import Prelude.Linear hiding (partition)
import qualified Control.Functor.Linear as Linear
import Control.Functor.Linear (State, state, execState, pure)
import qualified Data.Array.Mutable.Linear as Array
@ekmett
ekmett / Lies.hs
Last active September 21, 2020 04:13
Giving Int a Promotion
{-# Language RankNTypes #-}
{-# Language PolyKinds #-}
{-# Language DataKinds #-}
{-# Language PatternSynonyms #-}
{-# Language ViewPatterns #-}
{-# Language RoleAnnotations #-}
{-# Language StandaloneKindSignatures #-}
{-# Language TypeFamilies #-}
{-# Language TypeOperators #-}
{-# Language GADTs #-}
@serras
serras / variants.md
Created August 31, 2020 19:37
Variants: the ultimate frontier

Variants: the ultimate frontier

Most data serialization formats, like JSON, YAML, and EDN, feature a similar set of basic building blocks, namely:

  • Some primitive values, like numbers, strings, and booleans;
  • Key-value pairs, also known as maps, dictionaries, or objects;
  • Sequences, usually in the form of lists or arrays, and sometimes also sets.

I completely agree with the fact that those are basic building blocks for data inside any information system. However, as a Haskeller I always miss one additional part of my toolbox: variants. Variants are essentially tagged values which contain further values inside.

@sjoerdvisscher
sjoerdvisscher / nucleus.hs
Last active September 25, 2021 06:36
Nucleus of a profunctor
{-# LANGUAGE
PolyKinds
, RankNTypes
, TypeFamilies
, TypeOperators
, TypeApplications
, FlexibleInstances
, ScopedTypeVariables
, AllowAmbiguousTypes
, MultiParamTypeClasses
@monadplus
monadplus / running-haskell-scripts.md
Last active February 10, 2023 19:46
Running a haskell script without GHC using nix

Updated version can be found on my website.

Running a haskell script without GHC

Given the following haskell script generate-random-samples.hs that requires mwc-random ...

{-# LANGUAGE ScopedTypeVariables #-}

import System.Random.MWC

Monads and delimited control are very closely related, so it isn’t too hard to understand them in terms of one another. From a monadic point of view, the big idea is that if you have the computation m >>= f, then f is m’s continuation. It’s the function that is called with m’s result to continue execution after m returns.

If you have a long chain of binds, the continuation is just the composition of all of them. So, for example, if you have

m >>= f >>= g >>= h

then the continuation of m is f >=> g >=> h. Likewise, the continuation of m >>= f is g >=> h.

@monadplus
monadplus / profiling_haskell.md
Last active May 2, 2024 19:41
Haskell: Profiling

Profiling in Haskell

Do not get bogged down in microoptimizations before you've assessed any macro optimizations that are available. IO and the choice of algorithm dominate any low level changes you may make. In the end you have to think hard about your code!

Before starting to optimize:

  1. Is the -O2 flag on ?
  2. Profile: which part of the code is the slow one.
  3. Use the best algorithm in that part.
  4. Optimize: implement it in the most efficient way.