Skip to content

Instantly share code, notes, and snippets.

View fxn's full-sized avatar

Xavier Noria fxn

View GitHub Profile

Ruby: The future of frozen string literals

What is a literal?

In programming languages, literals are textual representations of values in the source code. This is a syntactical concept.

Some examples:

7 # integer literal
@fxn
fxn / foo.cr
Last active May 10, 2024 20:36
# crystal build foo.cr --release
# ./foo 37.7749 -122.4194 34.0522 -118.2437
#
# Around 0.08s.
module GeoCalculator
EARTH_RADIUS_IN_KM = 6371.0
def self.distance(lat1, lon1, lat2, lon2)
dlat = lat2 - lat1
docker run \
-d \
--name=mysql-test-truncation \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_ROOT_HOST=% \
-p 3307:3306 \
mysql/mysql-server:latest
threads = []
5.times do
threads << Thread.new {
p require 'date'
}
end
threads.each(&:join)
autoload :Date, 'date'
module Kernel
alias_method :original_require, :require
def require(path)
puts "Require invoked for #{path}"
original_require(path).tap do |loaded|
if loaded
require 'find'
require 'ptools'
frequencies = Hash.new(0)
Find.find('.') do |path|
basename = File.basename(path)
if basename.start_with?('.')
Find.prune if basename == '.git'
import std/[os,setutils]
const
markerLen = 4
let datastream = readFile(paramStr(1))
for i in 0 .. (high(datastream) - markerLen):
let chunk = datastream[i ..< i + markerLen]
if chunk.toSet.card == markerLen:
import std/[os,sequtils,strutils]
type
Assingment = tuple[fromSection, toSection: uint]
func toAssingment(rangeStr: string): Assingment =
let pair = rangeStr.split('-')
result.fromSection = parseUInt(pair[0])
result.toSection = parseUInt(pair[1])
import std/[os,sequtils,sets,sugar]
type
Rucksack = object
compA: HashSet[char]
compB: HashSet[char]
func toRucksack(rucksackStr: string): Rucksack =
let items = distribute(rucksackStr.toSeq, 2)
result.compA = items[0].toHashSet
import std/os
type
Shape = enum Rock, Paper, Scissors
GameResult = range[-1..1]
func toShape(shapeChr: char): Shape =
case shapeChr:
of 'A', 'X': Rock
of 'B', 'Y': Paper