C#:
public static B Bracket<A, B>(Func<A> before, Action<A> after, Func<A, B> during);Scala:
def bracket[A, B](before: => A, after: A => Unit)(during: A => B): B| namespace Samples.FSharp.TestTypeProvider | |
| open System.Reflection | |
| open Microsoft.FSharp.Core.CompilerServices | |
| open Samples.FSharp.ProvidedTypes | |
| open System.IO | |
| open Microsoft.FSharp.Quotations | |
| [<TypeProvider>] | |
| type public TypeProvider1() as this = |
C#:
public static B Bracket<A, B>(Func<A> before, Action<A> after, Func<A, B> during);Scala:
def bracket[A, B](before: => A, after: A => Unit)(during: A => B): BEach of these commands will run an ad hoc http static server in your current (or specified) directory, available at http://localhost:8000. Use this power wisely.
$ python -m SimpleHTTPServer 8000| object RefactorPuzzle { | |
| case class IntRdr[+A](read: Int => A) { | |
| def map[B](f: A => B): IntRdr[B] = | |
| IntRdr(f compose read) | |
| def flatMap[B](f: A => IntRdr[B]): IntRdr[B] = | |
| IntRdr(n => f(read(n)).read(n)) | |
| } | |
| object IntRdr { |
We are surveying developers at a number of sites to find their favourite programming language. Each response from a site is stored in a type that contains the site name, and a map/dictionary of programming languages and the number of votes they received. In C# this could look something like this:
class SurveyResponse {
public string Site;
public Dictionary Results;| case class MyOption[A](o: Option[A]) { | |
| def map[B](f: A => B): MyOption[B] = | |
| MyOption(o map f) | |
| def flatMap[B](f: A => MyOption[B]): MyOption[B] = | |
| MyOption(o flatMap (f(_).o)) | |
| def coflatten: MyOption[MyOption[A]] = | |
| MyOption(o match { | |
| case None => None |
The below is copy-pasted from a bunch of tweets. It's hard to succintly link them all, but you can pick it up from here:
At work, we don't apply DRY to HTML and CSS. Today I realized why: they don't compose cleanly.
| class Outer { | |
| class Inner | |
| type Type | |
| } | |
| trait Trait | |
| object Object extends Outer { | |
| val inner = new Inner | |
| } | |
| class OuterP[A] { | |
| class InnerP[B] |
Have you ever had to write code that made a complex series of succesive modifications to a single piece of mutable state? (Almost certainly yes.)
Did you ever wish you could make the compiler tell you if a particular operation on the state was illegal at a given point in the modifications? (If you're a fan of static typing, probably yes.)
If that's the case, the indexed state monad can help!
Motivation
| import Data.Tree | |
| import Control.Comonad | |
| import Control.Comonad.Trans.Class | |
| import Control.Comonad.Trans.Env | |
| replusify:: MonadPlus m => [a] -> m a | |
| replusify = msum . map return | |
| class Treeish l where | |
| children :: MonadPlus m => l -> m l |