Skip to content

Instantly share code, notes, and snippets.

@edsko
edsko / CheckedRevisited.hs
Last active November 3, 2021 08:35
Lightweight checked exceptions in Haskell without `unsafeCoerce`
{-# OPTIONS_GHC -Wall -fno-warn-unused-binds #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE IncoherentInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
#if __GLASGOW_HASKELL__ >= 708
{-# LANGUAGE RoleAnnotations #-}
#endif
@edsko
edsko / Checked.hs
Last active October 26, 2016 19:15
Lightweight checked exceptions in Haskell
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE AllowAmbiguousTypes #-}
#endif
-- | Lightweight checked exceptions
@Tarmil
Tarmil / gist:98cb7a0fb78e22aa4fde
Last active November 28, 2016 14:02
Compile a quotation with WebSharper.Compiler
// NuGet reference: WebSharper.Compiler
open WebSharper
type AR = IntelliFactory.Core.AssemblyResolution.AssemblyResolver
module FE = WebSharper.Compiler.FrontEnd
let compile (expr: Microsoft.FSharp.Quotations.Expr) : string option =
let loader = FE.Loader.Create (AR.Create()) (eprintfn "%O")
let options =
{ FE.Options.Default with
@derekmorr
derekmorr / gist:dd2ae28fdd5aced2d727
Created July 9, 2015 03:34
Structural typing in Scala
case class Circle(radius: Double) {
def area = radius * radius * Math.PI
}
case class Rectangle(side: Double) {
def area = side * side
}
case class Triangle(base: Double, height: Double) {
def area = 0.5d * base * height
@CMCDragonkai
CMCDragonkai / free_monad_interpreter_pattern.md
Last active October 4, 2024 14:39
Haskell: Free Monad + Interpreter Pattern

Free Monad + Interpreter Pattern

It's like creating the front end and back end of a compiler inside Haskell without the need of Template Haskell!

Write your DSL AST as a Free Monad, and then interpret the monad any way you like.

The advantage is that you get to swap out your interpreter, and your main code

@edsko
edsko / callbacks.hs
Last active January 14, 2019 06:43
Alleviating callback hell in Haskell
{-------------------------------------------------------------------------------
Discussion of ContT in terms of callbacks
For an alternative exposition, see
<http://www.haskellforall.com/2012/12/the-continuation-monad.html>.
-------------------------------------------------------------------------------}
{-# OPTIONS_GHC -Wall #-}
import Control.Exception
@ninegua
ninegua / scrapper.hs
Created April 22, 2015 04:37
Weibo Scrapper (using conduit == 0.2.*) written in 2012
{-# LANGUAGE OverloadedStrings, RankNTypes, ScopedTypeVariables,
NoMonomorphismRestriction, DeriveDataTypeable #-}
module Main where
import Prelude hiding (and, catch)
import Data.Char (toLower)
import Data.Conduit
import Data.Conduit.Util
import Data.Conduit.ImageSize (sinkImageInfo)
import Data.Conduit.Binary (sourceFile, conduitFile, sinkFile)
@antopor
antopor / StreamPipe.cs
Created March 7, 2015 04:14
How to redirect Process' standart input/output (C#, .net 4.5, async code)
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Test
{
class Program
{
@MiyamonY
MiyamonY / porland.hs
Created March 6, 2015 09:28
haskell monad porland
import Control.Monad
import Data.List
import Test.HUnit
readMaybe :: (Read a) => String -> Maybe a
readMaybe st = case reads st of [(x, "")] -> Just x
_ -> Nothing
foldingFunction :: [Double] -> String -> Maybe [Double]
foldingFunction (x:y:ys) "*" = return $ (y * x) : ys
@aryairani
aryairani / abstracting-over-monads.md
Last active October 25, 2016 03:38
Abstracting over Monads

Abstracting over Monads

We're going to do some computation.

def compute(x: Int, y: Int): Int = x + y

But we want to compute over Option: