Skip to content

Instantly share code, notes, and snippets.

View shwars's full-sized avatar

Dmitri Soshnikov shwars

View GitHub Profile
@shwars
shwars / blog-deeppavlov-covid-1.py
Created April 29, 2020 10:30
blog-deeppavlov-covid-1
from os.path import basename
def get_text(s):
return ' '.join([x['text'] for x in s])
os.makedirs('text',exist_ok=True)
for fn in glob.glob('noncomm_use_subset/pdf_json/*'):
with open(fn) as f:
x = json.load(f)
nfn = os.path.join('text',basename(fn).replace('.json','.txt'))
@shwars
shwars / ListPermute.fsx
Created October 1, 2019 08:31
Lists permutations
let rec ins z = function
| [] -> [[z]]
| x::xs ->
let res = ins z xs
let res1 = List.map (fun t -> x::t) res
(z::x::xs)::res1
let rec ins z = function
| [] -> [[z]]
| x::xs as L ->
@shwars
shwars / ShortSoltion.fsx
Created September 17, 2016 13:22
Short solution to F# Coding Dojo
open System
open System.IO
let read fn = File.ReadAllLines(sprintf "%s\%s" __SOURCE_DIRECTORY__ fn)
let data = read @"trainingsample.csv"
type Example = { Label:int; Pixels:int[] }
let prepare (x:string[]) =
let res =
x.[1..]
|> Array.map (fun x-> x.Split(',')|>Array.map int)
@shwars
shwars / EvalApplyRec
Last active May 28, 2016 11:06
Eval-Apply Lambda Interpreter with Recursion
type id = string
type expr =
| Var of id
| Lam of id*expr
| App of expr*expr
| Int of int
| Cond of expr*expr*expr
| Let of id*expr*expr
| LetRec of id*expr*expr
| PFunc of id
open System
let L = 14. // Space between wheels || Your values might be quite different
let R = 2.7 // Wheel's radius ||
let ticksPerRotation = 12000. // ~ amount of ticks is taken by one full rotation
let desiredAngle = int ((L/R/4.) * ticksPerRotation) // Math routine to calculate how many ticks is taken to turn
type Direction = Forward | Backward | Left | Right
// <Twitter boilerplate part>
@shwars
shwars / ContMonad
Created April 24, 2014 22:03
Continuation Monad
// Simple computations
let read() = System.Int32.Parse(System.Console.ReadLine())
let double x = x*2
let print x = printfn "%d" x
(read >> double >> print)()
@shwars
shwars / gist:11270960
Created April 24, 2014 21:58
Монада недетерминированных вычислений
(* Nondeterministic *)
type Nondet<'a> = 'a list
let ret x = [x]
let ret' x = x
let fail = []
let (>>=) mA b = List.collect b mA
ret' [1;2;3] >>= fun x ->
[x+1;x+2]
@shwars
shwars / gist:11270852
Created April 24, 2014 21:54
Монада Maybe и мотивирующий пример с вводом-выводом
open System
let read() =
printf ">"
let s = Console.ReadLine()
try
Some((int)s)
with
_ -> None
@shwars
shwars / Generators.fsx
Created March 17, 2014 07:20
Demonstration of creating generators in F#
// Simple counter
type cell = { mutable content : int }
let new_counter n =
let x :cell = { content = n }
fun () ->
(x.content <- x.content+1; x.content)
// The same, using F# refs
let new_counter' n =
@shwars
shwars / SimpleFileProcessing.fsx
Created March 17, 2014 06:56
Demonstrating how to process text file using sequences in F#
open System.IO
let ReadLines fn =
seq { use inp = File.OpenText fn in
while not(inp.EndOfStream) do
yield (inp.ReadLine())
}
#load @"FSharpChart.fsx"
#load @"FSharpChartAutoDisplay.fsx"
open Samples.Charting