Skip to content

Instantly share code, notes, and snippets.

View soareschen's full-sized avatar
🏠
Working from home

Soares Chen soareschen

🏠
Working from home
View GitHub Profile
// This is an example use of the extensible builder pattern in Context-Generic Programming (CGP)
// to progressively build a struct with missing fields filled with default values.
//
// This is in response to the question at https://www.reddit.com/r/rust/comments/1m4d8a8/how_to_handle_default_values_for_parameters/.
// More info at https://contextgeneric.dev/blog/extensible-datatypes-part-1/
use cgp::core::field::CanFinalizeWithDefault;
use cgp::prelude::*;
#[derive(Default)]
// This is a response on how to use Context-Generic Programming (CGP)
// to solve the specialization problem in the following blog post:
// https://oakchris1955.eu/posts/bypassing_specialization/
//
// More info available at https://contextgeneric.dev/.
use cgp::core::error::ErrorTypeProviderComponent;
use cgp::prelude::*;
// Redesign the FileSystem trait with CGP
@soareschen
soareschen / cgp-cake.rs
Created December 25, 2024 20:17
Scala Cake in CGP
// This is an example of how the Scala cake pattern can be used in CGP,
// following the example at https://www.baeldung.com/scala/cake-pattern
pub mod traits {
use std::collections::BTreeMap;
use anyhow::Error;
use cgp::prelude::*;
// For simplicity of the example, we use `dyn` trait to represent test cases
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHVMj2JxwEJc8zpHLSD3T1memwQ/tkCJk5Rf2zseCvpv soares@soares-desktop

Keybase proof

I hereby claim:

  • I am soareschen on github.
  • I am soareschen (https://keybase.io/soareschen) on keybase.
  • I have a public key ASBFlptlH9b-PeBx6_1vQOQoQ-_rUPKhr-kSRwSkf4znswo

To claim this, I am signing this object:

@soareschen
soareschen / dict-typing.hs
Last active April 8, 2018 07:59
Dict Typing - Duck Typing in Haskell using dictionaries and implicits
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
import GHC.Exts
-- Dict Typing - Duck Typing in Haskell using dictionaries and implicits
@soareschen
soareschen / implicit-parameter.hs
Last active March 29, 2018 01:38
Implicit Parameter Experiments
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE ConstraintKinds #-}
import GHC.Exts
-- A handler holds a function that takes in any a that
-- satisfies constraint p. Note that we are simply returning
-- string in here for the sake of simplifying the demo.
@soareschen
soareschen / has-field.hs
Created March 24, 2018 15:35
Experiment for simple implementation of HasField for extensible records
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE MultiParamTypeClasses #-}
@soareschen
soareschen / row-polymorphic-composition.purs
Created February 12, 2018 11:34
Composition of row polymorphic functions in PureScript
module Main where
import Prelude
import Data.Record
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
fooHandler :: forall r. { foo :: String | r } -> String
fooHandler args = "(foo-handler " <> args.foo <> ")"