Skip to content

Instantly share code, notes, and snippets.

View Ciantic's full-sized avatar

Jari Pennanen Ciantic

View GitHub Profile
@Ciantic
Ciantic / python-like-decorators-currying-in-rust.rs
Created February 11, 2023 17:05
Two examples: How to decorate (curry) a function and how to curry a method in Rust
// Cargo.toml
//
// [dependencies]
// macro_rules_attribute = "0.1"
#[macro_use]
extern crate macro_rules_attribute;
// How to decorate a function
@Ciantic
Ciantic / fetc.sh
Created December 22, 2022 20:54
fetch replies
#!/bin/bash
HEADERS='Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
curl -H "$HEADERS" "https://mastodon.social/users/Gargron/statuses/109543381373981313/replies?only_other_accounts=true&page=true" | jq
@Ciantic
Ciantic / register-reusable-blocks-as-patterns.php
Created September 27, 2022 16:16
Register reusable blocks as patterns
<?php
// Show menu item for reusable blocks
add_action('admin_menu', function () {
add_menu_page(_x('Reusable blocks', 'post type general name'), _x('Reusable blocks', 'post type general name'), 'edit_posts', 'edit.php?post_type=wp_block', '', 'dashicons-editor-table', 22);
});
add_action("should_load_remote_block_patterns", function () {
// I chose `should_load_remote_block_patterns` action because it's triggered
@Ciantic
Ciantic / sqlite-worker-query.ts
Created June 26, 2022 22:44
SQLite worker snippet
const worker = new Worker("https://cdn.jsdelivr.net/npm/sql.js@1.7.0/dist/worker.sql-wasm.js");
let rpcid = 0;
async function query(sql: string, params: any, action = "exec") {
const id = rpcid++;
return new Promise<any>((res, rej) => {
const listener = (e: MessageEvent<any>) => {
if (e.data && e.data.id === id) {
worker.removeEventListener("message", listener);
res(e.data);
}
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
use Paytrail\SDK\Client;
use Paytrail\SDK\Exception\HmacException;
use Paytrail\SDK\Exception\ValidationException;
use Paytrail\SDK\Model\Address;
use Paytrail\SDK\Model\CallbackUrl;
use Paytrail\SDK\Model\Customer;
@Ciantic
Ciantic / sqlite3_update_hook.cs
Created February 11, 2022 22:36
This is test code I threw away, it looks like SQLite3 update hook is *per* connection, making it useless for subscribing for events between processes (even if they use same database).
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using SQLitePCL;
static public class Thing {
public static void ListenSqlite(DbContext context)
{
context.Database.OpenConnection();
var c = context.Database.GetDbConnection() as SqliteConnection;
SQLitePCL.raw.sqlite3_update_hook(c.Handle, test_func, new object());
<?php
// Disable article images that come from the content e.g. first <img /> tag
add_filter('the_seo_framework_image_generation_params', function( $params, $args, $context ) {
if (isset( $params['cbs']['content'])) {
unset($params["cbs"]["content"]);
}
return $params;
}, 11, 3);
@Ciantic
Ciantic / YAML-to-JSON-with-Rust.rs
Created February 3, 2021 16:39
YAML to JSON with Rust
// Yaml to JSON with Rust
// Dependencies serde_yaml and serde_json
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3ddd4d9a65f03b717eeb1258299e9503
fn main() {
let json_value: serde_json::Value = serde_yaml::from_str(&"
---
test: some value here
another: value here
third:
@Ciantic
Ciantic / intersectionObserverSteps.js
Last active October 30, 2021 14:44
Intersection observer steps
// steps(10) == [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] == length = 11
const steps = (r) => Array(r + 1).fill().map((_, i) => i / r); // with fill
const steps = (r) => ([...Array(r + 1)]).map((_, i) => i / r); // with splat
const steps = (r) => Array.apply(null, Array(r + 1)).map((_, i) => i / r); // If no splat
// Benchmark notes: third implementation fails with maximum stack size, first one is the fastest (with fill)
@Ciantic
Ciantic / createObservableMap.ts
Last active September 19, 2021 18:59
ObservableMap (from MobX) to solidjs (not sure if this is a good idea)
// LICENSE MIT, Jari Pennanen
// For all purposes, I also consider this public domain work.
import { untrack } from "solid-js";
import { createStore } from "solid-js/store";
/**
* Solid JS
*
* @returns Map like observable