Skip to content

Instantly share code, notes, and snippets.

View jeremycochoy's full-sized avatar

Jeremy Cochoy jeremycochoy

View GitHub Profile
@jeremycochoy
jeremycochoy / sample.rb
Created February 21, 2019 17:01
Split strings back into smaller chunks
coordinates = []
word = ""
polyline.each_byte do |b|
# Add the character b to the current chunk
word << b
next unless b < 0x5f # While we are below the _ ascii code
# Add the chunk to the list of coordinates represented by a string
coordinates << word
word = ""
end
@jeremycochoy
jeremycochoy / encode.rb
Created February 21, 2019 17:14
Encode polyline
def encode(points, precision = 1e5)
result = ""
last_lat = last_lng = last_timestamp = 0
# Convert each point (lat, lng, ts) to a string
points.each do |point|
# Convert to integer
lat = (point[0] * precision).round
lng = (point[1] * precision).round
timestamp = point[2]
# Compute the difference with the previous point
struct ListZipper<T> {
private var leftList: [T] = []
private var rightList: [T] = []
/// Cursor to the selected Track
var cursor: T? = nil
}
init(from list: [T] = []) {
cursor = list.first
rightList = list
}
/// Convert to a swift array
func toList() -> [T] {
if let c = cursor {
return leftList + [c] + rightList.reversed()
} else {
/// Select the left element if available
mutating func left() {
guard leftList.isNotEmpty else {return}
if let c = cursor {rightList.append(c)}
cursor = leftList.popLast()
}
/// Select the right element if available
mutating func right() {
/// Insert a new track
mutating func insert(element: T) {
if let c = cursor {leftList.append(c)}
cursor = element
}
/// Remove the current pointed track
mutating func remove() {
guard let _ = cursor else {return}
/// Map a function to the tracks
func map<R>(f: (T) -> R) -> ListZipper<R> {
var zipper = ListZipper<R>()
zipper.leftList = leftList.map(f)
zipper.cursor = cursor.map(f)
zipper.rightList = rightList.map(f)
return zipper
}
var zippy = ListZipper<String>(from: ["I", "love", "pancakes"])
print(zippy.cursor!) // I
zippy.right() // move to the right
print(zippy.cursor!) // love
zippy.cursor = "hate"
print(zippy.toList().joined(separator: " ")) // I hate pancakes
zippy.remove()
zippy.cursor = "Eat"
print(zippy.toList().joined(separator: " ")) // Eat pancakes
import onnxruntime as rt
import onnx
import numpy as np
import torch
import torch.nn as nn
import torch.nn
import torch.onnx
def onnx_export(model: torch.nn.Module, input_shape, filename: str,
@jeremycochoy
jeremycochoy / fft-ios.swift
Created February 7, 2020 20:41
How to use Apple's Accelerate FFT in swift
//
// MIT LICENSE: Copy past as much as you want :)
//
// Your signal, array of length 1024
let signal: [Float] = (0 ... 1024)
// --- INITIALIZATION
// The length of the input
length = vDSP_Length(signal.count)