Skip to content

Instantly share code, notes, and snippets.

@imsaravana369
imsaravana369 / SplitOnPeriod.purs
Created July 16, 2024 16:26
Given a dot-separated Symbol, the typeclass `SplitOnPeriodWrap` will give the last segment of the dot separated symbol.
module Split where
import Prelude
import Data.Foldable (fold)
import Effect (Effect)
import Effect.Console(log)
import Effect.Unsafe
import Type.Proxy
import Prim.Symbol as Symbol
module UseConfig where
import Data.Symbol (class IsSymbol, reflectSymbol)
import Effect (Effect)
import Foreign (F, Foreign)
import Prelude
import Split (class SplitOnPeriodWrap) -- [code here](https://gist.github.com/imsaravana369/bbc01f20bef24b6f4f97a365c6bb0410)
import Type.Proxy (Proxy(..))
import Control.Monad.Except (runExcept)
@imsaravana369
imsaravana369 / retry-aff.purs
Last active October 6, 2023 15:39
An example snippet that shows how api can be retried in a neat way using the `purescript-aff-retry` module
module Main where
-- To Try in trypurescript editor : https://shorturl.at/ghqw9
import Prelude
import Effect
import Effect.Aff.Retry
import Effect.Aff
import Data.Time.Duration(Seconds(..))
import Data.Newtype(wrap)
import Data.Validation.Semigroup
type Person = {name :: String, age :: Int, email :: String}
validateName :: String -> V (Array String) String
validateName name = if null name
then invalid ["Name shouldn't be empty"]
else pure name
validateAge :: Int -> V (Array String) Int
// Just a simple example to understand how JS object is decoded to purescript record.
// Skipping unwrapSingleArguments, unwrapSingleConstructors, sumEncoding.
purescript_obj = { "firstName" : "Saravana", "lastName" : "Murugesan"}
foreign_obj = { "first_name" : "Saravana", "last_name" : "Murugesan"}
const camelToSnakeCase = str => str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
function decode(foreign_obj , res,options){
if (typeof res === 'object'){ // just a generalization
@imsaravana369
imsaravana369 / STRef_example.purs
Created May 20, 2023 15:42
Factorial function using ST Monad
module Main where
import Prelude
import Effect (Effect)
import Effect.Console (logShow)
import Control.Monad.ST.Internal(ST,for,while,foreach,new,run,write,read,modify)
import Data.Array((..))
@imsaravana369
imsaravana369 / areaUnderhill.py
Created December 18, 2022 16:59
Area Under the hill
def solve(self, A):
A.append(0)
n = len(A)
res = 0
x = 1
for i in range(1,n):
res += x*A[i]
x+=1
x = 1
class Dog{
String name;
Dog(this.name);
}
class Labrador extends Dog{
Labrador(String name):super(name);
}
class Doberman extends Dog{
Doberman(String name):super(name);
}
void main() {
String jsonString = r'{"name":"Saravanan","age":20}';
Map<String,dynamic> jsonMap = json.decode(jsonString);
Person p = Person.fromJson(jsonMap);
print("${p.name}, ${p.age}"); //prints Saravanan, 20
}
@imsaravana369
imsaravana369 / named_constructor_example.dart
Last active June 28, 2021 10:08
Dart Named Constructor example
class Person {
late final String name;
late final int age;
Person(this.name,this.age);
Person.fromJson(Map<String,dynamic> json){
this.name = json['name'];
this.age = json['age'] as int;
}
}