Skip to content

Instantly share code, notes, and snippets.

Avatar

Peter peheje

View GitHub Profile
@peheje
peheje / evo.go
Last active Sep 18, 2022
Differential evolution in Go
View evo.go
// Keep updated here https://gist.github.com/peheje/ea1d067803c8bb2a9fdbbc17b763174a
package main
import (
"fmt"
"math"
"math/rand"
"time"
@peheje
peheje / csvReader.fs
Last active Sep 10, 2022
Simple CSV reader F#
View csvReader.fs
open Microsoft.VisualBasic.FileIO
let read (path: string) =
let rec loop (parser: TextFieldParser) output =
if parser.EndOfData then output |> List.rev
else loop parser ((parser.ReadFields()) :: output)
let parser = new TextFieldParser(path, Delimiters = [|","|], HasFieldsEnclosedInQuotes = true)
loop parser []
View integral.kt
fun main() {
}
typealias Function = (x: Double) -> Double
typealias Rule = (f: Function, x: Double, h: Double) -> Double
fun leftRectangle(f: Function, x: Double, h: Double) = f(x)
fun midRectangle(f: Function, x: Double, h: Double) = f(x + h / 2.0)
fun rightRectangle(f: Function, x: Double, h: Double) = f(x + h)
@peheje
peheje / integration.kt
Created Oct 17, 2021
Numerical integration with Kotlin and some lambda passing
View integration.kt
fun main() {
}
// f is the function being integration, x is at which point, h is the size of each iteration along the x-axis
typealias Function = (x: Double) -> Double
typealias Rule = (f: Function, x: Double, h: Double) -> Double
fun leftRectangle(f: Function, x: Double, h: Double) = f(x)
fun midRectangle(f: Function, x: Double, h: Double) = f(x + h / 2.0)
@peheje
peheje / Automata.fs
Last active Sep 25, 2022
Simple automata
View Automata.fs
// http://atlas.wolfram.com/01/01/
let on = 1uy
let off = 0uy
let print xs =
for x in xs do if x = on then printf "" else printf ""
printf "\n"
let pqr (xs: byte array) i = (xs[i-1], xs[i], xs[i+1])
let rule0 (p, _, r) = (p + r) % 2uy
View Lego Mindstorms PID
from mindstorms import MSHub, Motor, MotorPair, ColorSensor, DistanceSensor, App
from mindstorms.control import wait_for_seconds, wait_until, Timer
from mindstorms.operator import greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to, equal_to, not_equal_to
import math
hub = MSHub()
motors = MotorPair('E', 'A')
roll_target = 89
kp = 15.0
@peheje
peheje / jobWrapping.kt
Created Aug 27, 2020
Wrap job multiple times
View jobWrapping.kt
import kotlin.random.Random
import kotlin.system.measureTimeMillis
fun main() {
val basicJob = BasicJob()
val delayedJob = DelayedJob(basicJob, 1000)
val measuredAndDelayedJob = MeasuredJob(delayedJob)
val measuredDelayedAndSynchronizedJob = SynchronizedJob(measuredAndDelayedJob, "lock")
measuredDelayedAndSynchronizedJob.run()
}
View bloomfilter.kt
import java.lang.Exception
import java.nio.ByteBuffer
import java.security.MessageDigest
import java.util.*
import kotlin.math.abs
fun main() {
val filterSize = 1_000_000
val numberOfEntries = 100_000
View add spotify albums to one playlist
import pyautogui
import time
time.sleep(2)
def record():
while True:
currentMouseX, currentMouseY = pyautogui.position()
print("(" + str(currentMouseX) + "," + str(currentMouseY) + ")")
time.sleep(3)
@peheje
peheje / main.kt
Created May 22, 2017
kotlin genetic
View main.kt
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.runBlocking
import kotlinx.coroutines.experimental.sync.Mutex
import java.util.*
import kotlin.system.measureTimeMillis
// BY peheje@github
fun stringToIntArray(str: String): IntArray {