Skip to content

Instantly share code, notes, and snippets.

View heyrutvik's full-sized avatar

Rutvik Patel heyrutvik

View GitHub Profile
@heyrutvik
heyrutvik / aa-tree.scala
Last active July 3, 2024 05:30
Implementation of AA Tree
package ds.tree.aa
import scala.annotation.tailrec
// https://en.wikipedia.org/wiki/AA_tree
sealed trait Tree
final case class Node(var key: Int, var level: Int, var left: Tree, var right: Tree) extends Tree
@heyrutvik
heyrutvik / executor_future_poll_waker.rs
Last active January 18, 2023 16:44
The dance of polling and waking in Rust!
// Cargo.toml
//
// [dependencies]
// futures = "0.3.25"
use std::future::Future;
use std::ops::Deref;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::{Receiver, sync_channel, SyncSender};
@heyrutvik
heyrutvik / SQLBuilder.scala
Created December 6, 2020 06:34
SQL (insert) query builder snippet using shapeless from Scala case classes
package example
import java.util.UUID
import shapeless.labelled.FieldType
import shapeless.ops.hlist.MapFolder
import shapeless.{HList, LabelledGeneric, Poly1, Witness}
sealed trait Table extends Product {
type Id
@heyrutvik
heyrutvik / Matrix.scala
Created December 5, 2020 10:56
matrix multiplication
package math
import cats.instances.list._
import cats.syntax.all._
import io.circe.generic.auto._
import io.circe.parser._
import scala.io.Source
/**
package example
import shapeless._
import shapeless.labelled.FieldType
object LabelledDerivation extends App {
import JsonObjectEncoder._
println {
JsonEncoder[IceCream].encode(IceCream("Sunday", 25))
@heyrutvik
heyrutvik / TypeclassDerivation.scala
Last active December 3, 2020 13:52
deriving typeclass instances using shapeless
package example
import shapeless._
object TypeclassDerivation extends App {
println {
CsvEncoder.writeCsv(IceCream("Sunday", 25))
// "Sunday,25"
}
@heyrutvik
heyrutvik / Demo.scala
Created March 12, 2020 18:18
Code sample from the talk: High Wizardry in the Land of Scala by Daniel Spiewak
package demo
//talk: https://vimeo.com/28793245
//Kind
class HOMap[K[_], V[_]](delegate: Map[K[Any], V[Any]]) {
def apply[A](key: K[A]): V[A] =
delegate(key.asInstanceOf[K[Any]]).asInstanceOf[V[A]]
}
@heyrutvik
heyrutvik / Matrix.idr
Created January 11, 2019 11:32
couple of matrix operations using dependent type
import Data.Vect
-- matrix representation using vectors
Matrix : (rows : Nat) -> (cols : Nat) -> (elem : Type) -> Type
Matrix rows cols elem = Vect rows (Vect cols elem)
-- add corresponding elements from two vectors
vector_op : Num elem => ((elem, elem) -> elem) -> Vect c elem -> Vect c elem -> Vect c elem
vector_op op av bv = let ts = zip av bv in map op ts
@heyrutvik
heyrutvik / lambdacalculus.scala
Created September 6, 2018 18:55
Explanation and Implementation of the Lambda Calculus using Scala
package lambdacalculus
/**
* Abstract Syntax (AST) for lambda terms
*
* In the simplest form of lambda calculus, terms are built using only the following rules:
*
* Variable (x)
* A character or string representing a parameter.
* Abstraction (λx.M)
@heyrutvik
heyrutvik / AppendixA.fs
Last active August 28, 2018 07:14
Appendix A (Crash Course in F#) exercises from the book Programming Language Concepts by Peter Sestoft
module AppendixA
// Programming Language Concepts by Peter Sestoft
// $ fsharpi
// > #load "AppendixA.fs";;
// > open AppendixA;;
// > let t = Br (1, Br (2, Lf, Lf), Br (3, Lf, Lf));;
// Exercise A.1