Instantly share code, notes, and snippets.

View Constrained.hs
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Constrained
( Constrained(unconstrained)
, constrain
) where
import Data.Bool ((&&), otherwise)
View Sized.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE TypeOperators #-}
module Data.Text.Sized
( Text
View Main.hs
module Main where
import Data.Bool (bool)
import Control.Monad ((<=<))
import Control.Monad.State (State, evalState, get, put)
import Data.Char (isSpace)
import Data.Foldable (traverse_)
import Data.Functor.Compose (Compose(Compose, getCompose))
import Data.Functor.Const (Const(Const, getConst))
import Data.Functor.Product (Product(Pair))
View Main.hs
module Main where
import Data.Functor (void)
import Control.Applicative ((<|>), empty, many, some, optional)
import Control.Monad (msum, replicateM)
import Control.Monad.State (StateT(runStateT))
import Control.Monad.State.Class (get, put)
import Data.Char (isHexDigit, chr, isDigit, isSpace)
import Data.Map (Map)
import Data.Maybe (listToMaybe, fromMaybe)
View Lens.hs
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RankNTypes #-}
module Main where
import Control.Applicative (Applicative(..))
import Data.Bool (Bool(False, True), not)
import Data.Char (toUpper)
import Data.Function (($), (&), (.), const, flip)
View pythagoreans.hs
module Main
( main
) where
import Control.Arrow ((&&&))
import Control.Monad.Trans.State (StateT(StateT), evalStateT)
import Data.List (uncons, unfoldr)
import Data.Maybe (fromMaybe, listToMaybe)
import System.Environment (getArgs)

Sharing behaviour between React components

React 0.13 introduced using plain Javascript classes to create components, 0.14 introduced "stateless functional components" as another method of defining them. Neither of these have an in-built method for behaviour sharing such as we had with the mixins option passed to createClass. So now we get to review mixins as a pattern for behaviour sharing and, if necessary, come up with something better. If they're not all bad, we'll need to figure out how to, or even if we can, add them to the two new component definition options.

The problem

Dan Abromov's medium article from Mar 2015 makes the case for avoiding mixins.

For me (paraphrasing in the extreme), the key problems exposed are:

  • mixins can unintentionally hide / distribute the source of behaviour
View alternative.hs
module Main where
import Control.Applicative
import Data.Monoid
fizz n
| n `mod` 3 == 0 = Just "Fizz"
| otherwise = Nothing
buzz n
View recursive-foldable.hs
{-# LANGUAGE DeriveFoldable #-}
module Main where
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Foldable
data Object a
= Value a