Skip to content

Instantly share code, notes, and snippets.

class Metrics
def self.euclidean_distance(a,b)
return unless a.any? && (a.size == b.size)
diff_squared = (0..a.size-1).reduce(0) do |sum, i|
sum + (a[i] - b[i])**2
end
Math.sqrt(diff_squared)
end
@turbod
turbod / ApiResponse.ts
Created July 17, 2023 06:51
Api response different types based on status
type ApiResponse<T> =
| { status: 'success'; data: T; timestamp: Date }
| { status: 'error'; message: string; timestamp: Date }
const response1: ApiResponse<number> = {
status: 'success',
data: 100,
timestamp: new Date(),
}
@turbod
turbod / Child.tsx
Last active July 17, 2023 06:45
Conditional Types
type Props = {
name: string
} & (MaleProps | FemaleProps)
type MaleProps = {
gender: 'male'
salary: number
}
type FemaleProps = {
// These styles will make sure the component
// is not visible, but will still be announced
// by screen readers.
//
// Adding “display: none” would hide the
// element from ALL users, including those
// using screen-readers.
const hiddenStyles = {
display: 'inline-block',
position: 'absolute',
IO.popen(["file", "--brief", "--mime-type", 'test.gif'], in: :close, err: :close) { |io| io.read(35).chomp }
@turbod
turbod / QEMU_ON_M1.md
Created February 12, 2023 13:20 — forked from citruz/QEMU_ON_M1.md
Create Ubuntu and Windows VMs with QEMU on Apple Silicon

Running Linux and Windows on M1 with QEMU

30.11.2020: Updated with the new patchseries and instructions for Windows

02.12.2020: Added tweaks

08.12.2020: Updated with patchseries v4

31.01.2020: Updated with patchseries v6

@turbod
turbod / calculator.rb
Created December 20, 2022 13:14
Simple calculator in ruby
operation = "1 + 1 + 20 - 3 "
Kernel.eval operation
# 19
raw_ops = operation.split /(\+|-)/
ops = raw_ops.map { |raw_op| Integer(raw_op, exception: false) || raw_op }
# [1, "+", 1, "+", 20, "-", 3]
while ops.size > 1
operator = ops.delete_at(1)
STDOUT.sync = true # DO NOT REMOVE
w, h = gets.split(" ").collect { |x| x.to_i }
n = gets.to_i # maximum number of turns before game over.
x0, y0 = gets.split(" ").collect { |x| x.to_i }
x1 = x0
y1 = y0
max_x = w - 1
min_x = 0
max_y = h - 1
STDOUT.sync = true # DO NOT REMOVE
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
# w: width of the building.
# h: height of the building.
@w, @h = gets.split(" ").collect {|x| x.to_i}
@n = gets.to_i # maximum number of turns before game over.
@x, @y = gets.split(" ").collect {|x| x.to_i}
path = URI.parse(params[:url]).path.presence || "/"
redirect_to path