Skip to content

Instantly share code, notes, and snippets.

@mzaks
mzaks / gen_lookup_tables.mojo
Created September 18, 2024 15:06
Generate lookup table for unicode case conversion
alias lower_special_2: String = """\
00DF; 00DF; 0053 0073; 0053 0053; # LATIN SMALL LETTER SHARP S
FB00; FB00; 0046 0066; 0046 0046; # LATIN SMALL LIGATURE FF
FB01; FB01; 0046 0069; 0046 0049; # LATIN SMALL LIGATURE FI
FB02; FB02; 0046 006C; 0046 004C; # LATIN SMALL LIGATURE FL
FB05; FB05; 0053 0074; 0053 0054; # LATIN SMALL LIGATURE LONG S T
FB06; FB06; 0053 0074; 0053 0054; # LATIN SMALL LIGATURE ST
0587; 0587; 0535 0582; 0535 0552; # ARMENIAN SMALL LIGATURE ECH YIWN
FB13; FB13; 0544 0576; 0544 0546; # ARMENIAN SMALL LIGATURE MEN NOW
FB14; FB14; 0544 0565; 0544 0535; # ARMENIAN SMALL LIGATURE MEN ECH
@mzaks
mzaks / crazy_string.mojo
Last active September 24, 2024 15:37
Mojo String with small string optimisation and unicode support (based on UTF-8)
from algorithm.functional import vectorize
from bit import bit_width, byte_swap, count_leading_zeros
from collections._index_normalization import normalize_index
from memory import memcpy, memset_zero
from sys import is_big_endian, sizeof
from utils import StringSlice, Span
from utils.string_slice import _utf8_byte_type, _StringSliceIter
struct CrazyString[
# automatically generated by the FlatBuffers compiler, do not modify
import flatbuffers
@value
struct MetadataVersion:
var _value: Int16
# 0.1.0 (October 2016).
alias V1 = 0
# 0.2.0 (February 2017). Non-backwards compatible with V1.
@mzaks
mzaks / json_parser.mojo
Last active June 5, 2024 09:56
Quick and Dirty JSON parser in Mojo
@value
struct JSONValue(CollectionElement):
var _text: String
fn __init__(inout self, text: String):
self._text = text.strip()
fn is_object(self) -> Bool:
return self._text.startswith("{") and self._text.endswith("}")
@mzaks
mzaks / main.mojo
Last active May 5, 2024 12:00
Branchless comparison
fn compare(s1: DTypePointer[DType.uint8], s2: DTypePointer[DType.uint8], count: Int)->Int:
var result = 0
var i = 0
while i < count:
var s1i = s1[i]
var s2i = s2[i]
var smaller = s1i < s2i
var bigger = s1i > s2i
i += 1 + count * int(smaller or bigger)
result = -1 * int(smaller) + 1 * int(bigger)
@mzaks
mzaks / main.mojo
Created April 26, 2024 04:57
Mojo Hasher POC
trait Hashable:
fn __hash__[H: Hasher](self, inout hasher: H):
...
trait Hasher:
fn __init__(inout self):
...
fn update(inout self, bytes: DTypePointer[DType.uint8], n: Int):
...
fn finish[dt: DType = DType.uint64](owned self) -> Scalar[dt]:
@mzaks
mzaks / plot_experience.py
Created January 19, 2024 07:28
Plot your experience
import matplotlib.pyplot as plt
def _normalized_experience(data: dict[str, list[int | list[int]]]) -> tuple[int, dict[str, list[int]]]:
"""Returns a tuple with latest year and an experience dict with normalised years list"""
latest_year = 0
result = {}
for topic, years in data.items():
normalised_years = []
for entry in years:
if isinstance(entry, list):
@mzaks
mzaks / checkout_remote_modules.sh
Created October 29, 2023 10:44
Poor persons module management for Mojo
#!/bin/bash
function check_out_remote_module() (
rurl="$1"
shift
declare -a paths
declare -a module_names
for var in "$@"
do
IFS="="
@mzaks
mzaks / fiby_tree.mojo
Created August 19, 2023 17:59
FibyTree Mojo
from Bit import bit_length
from String import String
from Vector import DynamicVector, UnsafeFixedVector
from List import VariadicList
struct FibyTree[T: AnyType, cmp: fn(T, T)->Int, to_str: fn(T) -> String]:
alias Union = 0
alias Intersection = 1
alias Difference = 2
alias SymetricDifference = 3
@mzaks
mzaks / nucleotides.mojo
Created May 23, 2023 06:19
Count nucleotides with Mojo
from DType import DType
from Functional import vectorize
from Pointer import DTypePointer
from String import String, ord
from TargetInfo import dtype_simd_width
alias simd_width_u8 = dtype_simd_width[DType.ui8]()
let dna = "CAAGAACCAAGATAACACTCATCGTTTACTTCTTACCCGTGCCAATTCGTATTACAAACGAAACCGTGTGGGCCATGTTCGTTATCCGAGGCCCCTTCAATTACTCGTCACTAGTGACCGTCGCTACTATGCCGTGTCCATGATATTACATCAAGACAATGAGATACGAAACGACAGCTGTTCCTACGCCTCGCGAGGGGTTCTACCCCTGAGCCGTGGGAACAGGCCGTCCGACGATCTTCAAGTGTTAAAGCTAGAAAACTTGATCAGAGAACAGTGACAATCCGGTGCAATTAGGGCGCTTCTAGCAAAGTCTTGACGGTTGACATGCTATTCTACCGGCGCAGGTTGCTTGAATGCGCGGGAGTTTTAAGCTCCTCTGTCACGCCATGCCCCCTGCAGTAGCTCACCAGCAAGAAGTTGGCTTAATATACCTGGTAGGAACGTTTGGTTAAACTTCTTTCCCTCTTCTTATACCGATGACACCTACCAATTACGGTCGGCCCGCCCGTGATCCAAACAGGCCTTAATCTTCCAATAATTCAATATGTGTGTGGCTTACAGGAGTCGAATATTTATAAGTGCATTCCTGCCTTCGCTGTTGCGATTTATAGCATCTTATGGTGGCGCAGGGCAACACTTAAAAGGGAGCCAACATGAGTTTCTAGCGTCAGGCACTGCCCTGAGGTAAAGGAATACCTGTTCGATACTATGAGGCGAGATCGCCCCACCTTAAAACAGAAAGACGGTAACGGTCCCTAGCCATTTCCTTATTGCGTACGAGATTATGGAACGCTT