Skip to content

Instantly share code, notes, and snippets.

@Ellyll
Ellyll / CreateDataTable.fs
Last active May 9, 2020 09:20
F# function that takes a sequence (e.g. List) and returns a DataTable (with support for the F# Option type)
open System
open System.Data
open System.ComponentModel
let createDataTable (data: seq<'T>) : DataTable =
let table = new DataTable()
let props = TypeDescriptor.GetProperties(typeof<'T>)
// Add columns
for prop in props do
@Ellyll
Ellyll / Program.fs
Last active March 16, 2020 13:30
F# console app to get Corona virus data for Wales by county
open FSharp.Data
[<Literal>]
let SampleUrl = "file://" + __SOURCE_DIRECTORY__ + "/Sample.html"
[<Literal>]
let PageUrl = "https://phw.nhs.wales/news/public-health-wales-statement-on-novel-coronavirus-outbreak/"
type Page = HtmlProvider<SampleUrl>
@Ellyll
Ellyll / cymharydd.fsx
Created January 19, 2020 22:02
Cymharu ffolderi gyda sgript F# (heb gorffen)
open System.IO
type MathLlwybr =
| File of Length: int64
| Directory
//let llwybr1 = fsi.CommandLineArgs.ElementAt(1)
let dirChars = [| Path.DirectorySeparatorChar ; Path.AltDirectorySeparatorChar |] |> Array.distinct
@Ellyll
Ellyll / text_replacer.rb
Last active September 11, 2018 07:41
Replace words in text files in a directory with replacements in csv file
require 'fileutils'
inputDirectory = "/home/ellyll/Dropbox/Arbrofion/TextReplacer"
outputDirectory = "/tmp/ReplacedFiles"
replacementsFile = "/home/ellyll/Dropbox/Arbrofion/TextReplacer/replacements.csv"
# Read replacements csv file
replacements = File.readlines(replacementsFile).collect {|line| line.chomp.split(",") }
replacements.each {|r| puts "replace \"#{r[0]}\" with \"#{r[1]}\"" }
@Ellyll
Ellyll / northwalestech.kt
Created March 12, 2017 14:17
North Wales Tech JVM Languages tasks
import com.eclipsesource.json.Json
import com.eclipsesource.json.JsonObject
import com.eclipsesource.json.JsonValue
// Recursively get JSON values for all properties in a JSON object with the given key name
fun getAllWithName(jsonObj : JsonObject, keyName : String) : ArrayList<JsonValue> {
val keys = jsonObj.names()
return keys.fold(ArrayList<JsonValue>()) { acc, key ->
val value = jsonObj.get(key)
if (key == keyName) {
@Ellyll
Ellyll / FizzBuzz.fsx
Created November 15, 2016 22:06
F# FizzBuzz attempt
let fizzBuzz n =
if n % 3 = 0 && n % 5 = 0 then "FizzBuzz"
elif n % 3 = 0 then "Fizz"
elif n % 5 = 0 then "Buzz"
else (sprintf "%d" n)
let a = seq { 1 .. 20 }
a
|> Seq.iter (fun n -> printfn "%d: %s" n (fizzBuzz n))
@Ellyll
Ellyll / haveibeenpwned.rb
Created February 15, 2014 16:03
Check a list of addresses on https://haveibeenpwned.com to see if they've been compromised
require 'net/http'
require 'json'
http = Net::HTTP.new('haveibeenpwned.com',443)
http.use_ssl = true
addresses = %w(foo@bar.com) # Add your addresses here separated by spaces
http.start
addresses.each do |email_address|
res = http.get("/api/v2/breachedaccount/#{email_address}")
using System.IO;
using System.Linq;
namespace SortingExample
{
public class Sorter
{
static void Main(string[] args)
{
var lines = File.ReadAllLines(args[0]).OrderBy(a => a);