Skip to content

Instantly share code, notes, and snippets.

View ShahOdin's full-sized avatar
🎋

Shah Saraei ShahOdin

🎋
View GitHub Profile
@ShahOdin
ShahOdin / getMostRecentRecord.sql
Last active August 30, 2020 17:49
returns the full row for "highest" record of some sort
CREATE TABLE link (
ID serial PRIMARY KEY,
name VARCHAR (255) NOT NULL,
other VARCHAR (255) NOT NULL,
score INT NOT NULL
);
INSERT INTO link (name, other, score)
VALUES
('Foo', 'ABC' , 13),
@ShahOdin
ShahOdin / demo.scala
Created February 5, 2020 20:44
fs2 stream error handling
import cats.effect.{ExitCode, IO, IOApp, Resource}
import scala.util.Random
object Demo extends IOApp {
var i = 0
def foo: IO[Int] = IO{
i = i + 1
i
}.flatTap(
j => IO{
@ShahOdin
ShahOdin / JFutureOps.scala
Created January 29, 2020 20:50
lifts Java Future to generic F using cats
import java.util.concurrent.Future
import cats.Applicative
import cats.effect.{Concurrent, Sync, Timer}
import cats.effect.concurrent.Deferred
import fs2.Stream
import cats.syntax.flatMap._
import cats.syntax.functor._
import scala.concurrent.duration.FiniteDuration
@ShahOdin
ShahOdin / kdTree.scala
Last active January 17, 2020 01:05
kdtree implementation
object Demo {
def getMedian(ints:List[Int]): Int = ints.sorted.apply(ints.length / 2)
//sorry no shapless here. all these lists have the same length. in two dimensions they will have length two.
case class Point(coordinates: List[Int])
object Point{
def in2D(x: Int, y: Int): Point = Point(List(x,y))
def estimateNearestPoint(points: List[Point], point: Point): Point = {
val indexOfPointWithSmallestDistance: Int = points
@ShahOdin
ShahOdin / demo.scala
Created December 11, 2019 16:33
cats concurrency and paralallisation demo
package com.itv.sif.db
import cats.effect.IOApp
import java.util.concurrent.Executors
import fs2.Stream
import cats.syntax.functor._
import scala.concurrent.ExecutionContext
import cats.effect._
@ShahOdin
ShahOdin / IorMonitoring.scala
Last active December 9, 2019 16:48
Content monitoring made easy with Ior
package com.itv.sif.db
import cats.{Order, Show}
import cats.data.{Ior, NonEmptyList, NonEmptySet, Validated}
import cats.kernel.Semigroup
import cats.syntax.apply._
import cats.syntax.option._
import cats.syntax.order._
import cats.syntax.show._
@ShahOdin
ShahOdin / contentMonitoringDemo.scala
Last active December 7, 2019 01:58
storing successes and failures over time
import cats.data.{Ior, NonEmptyList, NonEmptySet}
import io.circe.Json
object demo2 {
type TimeStamp = Long
type Identifier = Long //unique identifier, ie: ccid + streaming-platform perhaps?
type Reason = String
//Having two tables, storing only validated data in the Success table, and storing unavailability reasons in the other table.
@ShahOdin
ShahOdin / matchMaking.scala
Last active December 6, 2019 14:30
reconstructing state
package com.itv.sif.db
import cats.Show
import cats.data.{NonEmptyList, NonEmptySet, Validated}
import cats.syntax.apply._
import cats.syntax.either._
import cats.syntax.validated._
import cats.syntax.show._
import cats.instances.all._
import cats.kernel.Semigroup
@ShahOdin
ShahOdin / dataAggregationDemo.scala
Created December 4, 2019 23:11
Data aggregation
import cats.data.{NonEmptyChain, NonEmptyList, Validated, ValidatedNec}
import cats.syntax.apply._
import cats.syntax.option._
import cats.syntax.either._
import cats.instances.all._
//just a demonstration of aggregating partial updates in a flattened model
object partialUpdateBasicDemo extends App {
type A = String
@ShahOdin
ShahOdin / biggestPrimes.clj
Created November 30, 2019 23:58
clojure attempt
(defn is_divisible [n m] (= (mod n m) 0))
(defn is_prime[n]
(every?
#(not (is_divisible n %))
(range 2 (- n 1))))
(take-last 3 (filter is_prime (range 1 20)))