Skip to content

Instantly share code, notes, and snippets.

Avatar
🌚

Andrew / Kana kana-sama

🌚
View GitHub Profile
View gist:e08eba6a49bcbd17e5508954159e3662
As a gaming website we want to implement a tournament service with a feature called "Back a friend".
Each player holds certain amount of bonus points. Website funds its players with bonus points based on all kind of activity. Bonus points can traded to goods and represent value like real money.
One of the social products class is a social tournament. This is a competition between players in a multi-player game like poker, bingo, etc)
Entering a tournament requires a player to deposit certain amount of entry fee in bonus points. If a player has not enough point he can ask other players to back him and get a part the prize in case of a win.
In case of multiple backers, they submit equal part of the deposit and share the winning money in the same ration.
From a technical side, the following API service with 5 endpoints should be implemented
#1 Take and fund player account
GET /take?playerId=P1&points=300 takes 300 points from player P1 account
GET /fund?playerId=P2&points=300 funds (add to balance) player P2 wi
View MyIO.hs
{-# LANGUAGE BlockArguments #-}
import Control.Monad (ap, when)
import System.Exit (exitSuccess)
data MyIO a
= GetLine (String -> MyIO a)
| PutStrLn String (MyIO a)
| ExitSuccess
| Pure a
View Q.hs
{-# LANGUAGE ConstraintKinds, PatternSynonyms #-}
{-# LANGUAGE TypeApplications, TupleSections, ScopedTypeVariables #-}
{-# LANGUAGE UndecidableInstances, RankNTypes, DerivingStrategies,
FlexibleInstances, FlexibleContexts, TypeFamilies, DeriveGeneric,
GeneralizedNewtypeDeriving, DeriveTraversable, OverloadedStrings #-}
module Q
( defaultOptions,
genericDecode,
genericEncode,
View elim.hs
{-# LANGUAGE LambdaCase, BlockArguments, EmptyCase #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeSynonymInstances, TypeOperators, FlexibleInstances, FlexibleContexts, UndecidableInstances #-}
{-# LANGUAGE TypeFamilies, GADTs, MultiParamTypeClasses, FunctionalDependencies #-}
{-# LANGUAGE DataKinds, PolyKinds, AllowAmbiguousTypes, TypeApplications, ScopedTypeVariables #-}
import GHC.Generics
import Unsafe.Coerce (unsafeCoerce)
import Data.Type.Equality
View index_of_fib_with_50_digs.hs
import Data.List (findIndex)
import Data.Maybe (fromJust)
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
solution = fromJust $ findIndex (\x -> length (show x) == 50) fibs
View coroutine.hs
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, UndecidableInstances, LambdaCase #-}
import Control.Monad.State
import Control.Monad.Writer
import Data.Foldable
instance Monad m => Functor (CoroutineT m) where
fmap f m = CoroutineT $ do
result <- runCoroutineT m
View qwe.hs
{-# LANGUAGE DataKinds, DeriveGeneric, TypeApplications, AllowAmbiguousTypes, ScopedTypeVariables,
PolyKinds, TypeSynonymInstances, FlexibleInstances, OverloadedStrings, OverloadedLists, FlexibleContexts,
TypeOperators #-}
import Data.Aeson
import Data.Proxy
import Data.Typeable
import qualified Data.ByteString.Lazy.Char8 as BS
import qualified Data.Vector as V
import qualified Data.Text as T
View generic-lens-anon-records.hs
{-# LANGUAGE RankNTypes, TypeInType, TypeApplications, OverloadedLabels,
ScopedTypeVariables, TypeOperators, GADTs, FlexibleInstances, FlexibleContexts,
TypeFamilies, UndecidableInstances #-}
import Data.Kind (Type)
import Data.Proxy (Proxy(..))
import GHC.Generics
import GHC.TypeLits (Symbol, KnownSymbol, symbolVal)
import Control.Lens
import Data.Generics.Product.Fields (field')
View trans.hs
import Data.Kind
import Data.Bool (bool)
class Trans (ts :: [Type]) a b | ts -> a b where trans :: a -> b
instance Trans '[a] a a where trans = id
instance (Trans (b:ts) b c, Trans1 a b) => Trans (a:b:ts) a c where trans = trans @(b:ts) . trans1
class Trans1 a b where trans1 :: a -> b
instance Trans1 Int String where trans1 = show
instance Trans1 (Maybe ()) String where trans1 = show
View a-subs-tree.hs
{-# LANGUAGE RecursiveDo #-}
import Control.Lens ((^.), (+~), (-~), (.~))
import GHC.Generics (Generic)
import Data.Generics.Labels ()
import Control.Monad (forever)
import Control.Concurrent (forkIO, killThread, threadDelay)
import Data.Functor.Product (Product (..))
import System.Random (randomIO)
import Data.IORef (newIORef, modifyIORef', readIORef, writeIORef)
You can’t perform that action at this time.