Skip to content

Instantly share code, notes, and snippets.

View id-ilych's full-sized avatar

Ilya Denisov id-ilych

View GitHub Profile
@id-ilych
id-ilych / ParamReader.scala
Last active February 16, 2016 18:17
Helper methods for getting/applying request params
trait Request {
def getParam(key: String): String
def getCommaParamList(key: String): Seq[String]
def paramOpt[T](key: String)(implicit reader: ParamReader[T]): Option[T] = Option(getParam(key)).map(reader.read)
def paramOpt[T](key: String, op: T => _)(implicit reader: ParamReader[T]): Unit = paramOpt(key).foreach(op)
def param[T](key: String)(implicit reader: ParamReader[T]): T = reader.read(getParam(key))
def param[T](key: String, op: T => _)(implicit reader: ParamReader[T]): Unit = op(param(key))
def paramExists(key: String): Boolean = getParam(key) != null
def paramExists(key: String, op: Boolean => _): Unit = op(paramExists(key))
@id-ilych
id-ilych / EitherTest.scala
Last active April 29, 2019 14:43
Before/after (Either)
// before
override def receive(ev: AnyRef)(implicit self: ItemValue) = ev match {
case ev: MsgItems.ApplyAction if ev.getChoice == ActionChoice.Activate =>
if (ev.getArgsCount > 0) {
val arg = ev.getArgs(0)
cfg.targets.lift(arg) match {
case Some(target) =>
items.syncBroadcast(SyncToolForUpgrade(typeId = target.toolTypeId)).info match {
case Some(tool) =>
target.nextUpgrade(upgradesHave = tool.upgrades) match {
#include <functional>
#include <optional>
#include <vector>
#include <iostream>
template<typename T>
using Receiver = std::function<void( T )>;
template<typename T, typename U>
using Transformer = std::function<U( T )>;
@id-ilych
id-ilych / Fail.java
Created April 7, 2019 17:36
Contest dumb fail I've made :(
// what I carelessly wrote
BigInteger value = ciphertext[0];
BigInteger p, o;
if (value.equals(ciphertext[1])) { // << here's careless fail. Never should've done it
BigInteger[] factors = factorize(value);
p = factors[0];
o = factors[1];
} else {
p = gcd(value, ciphertext[1]);
o = value.divide(p);
@id-ilych
id-ilych / logcat.log
Created April 9, 2019 13:01
Cocos Creator 2.0.9 Android `wss://` issue
04-09 13:31:07.813: D/WebSocket.cpp(20710): WebSocket thread start, helper instance: 0xd19ad4e0
04-09 13:31:07.820: D/WebSocket.cpp(20710): scheme: ws, host: echo.websocket.org, port: 0, path:
04-09 13:31:07.926: D/WebSocket.cpp(20710): WebSocket (0xce292b00) Unhandled websocket event: 32
04-09 13:31:07.926: D/WebSocket.cpp(20710): WebSocket (0xce292b00) Unhandled websocket event: 29
04-09 13:31:08.166: D/WebSocket.cpp(20710): WebSocket (0xce292b00) Unhandled websocket event: 24
04-09 13:31:08.277: D/WebSocket.cpp(20710): WebSocket (0xce292b00) Unhandled websocket event: 2
04-09 13:31:08.277: D/WebSocket.cpp(20710): onConnectionOpened...: 0xce292b00, client protocols: , server selected protocol:
04-09 13:31:08.286: D/WebSocket.cpp(20710): Safely done, msg(2)!
04-09 13:31:08.287: D/WebSocket.cpp(20710): msg(2) append: 0 + 4 = 4
04-09 13:31:08.287: D/WebSocket.cpp(20710): msg(2) was totally sent!
build_prompt () {
local lhs=$1
local rhs=$2
local save='\e[s' # Save cursor position
local rest='\e[u' # Restore cursor to save point
# Strip ANSI commands before counting length
# From: https://www.commandlinefu.com/commands/view/12043/remove-color-special-escape-ansi-codes-from-text-with-sed
rhs_stripped=$(sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" <<<"$rhs")
echo "\[${save}\e[${COLUMNS:-$(tput cols)}C\e[${#rhs_stripped}D${rhs}${rest}\]${lhs}"
}
// WebSocketFactory.d.ts
export function newWebSocket( url: String ): WebSocket;
// WebSocketFactory.js
exports.newWebSocket = function( url ) {
if (CC_JSB) {
return new WebSocket( url, [], "ssl_cert.pem" );
}
else {
return new WebSocket( url );
@id-ilych
id-ilych / .jsx
Last active June 24, 2019 15:42
React Native "ref" playground
import * as React from 'react';
import { Button, Text, TextInput, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
// You can import from local files
import AssetExample from './components/AssetExample';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
/* eslint-disable no-console */
const config = require('../graphql-schema-linter.config.js')
// Aaa prefix is used to put this rule on top of the list
// which is required for excludes to work properly
// This rule allows to add exclusions (defined in graphql-schema.config.js)
function AaaWithExcludes (context) {
const stack = []
@id-ilych
id-ilych / geometry.js
Last active April 13, 2024 09:34
Functions to build a convex hull and minimum bounding box for a given set of points
const geometry = {};
// for all the functions below, assume screen coordinates: the x-axis is rightward, the y-axis is downward
// finds a point with the lowest vertical position (leftmost wins in case of a tie)
geometry.lowestPoint = (points) => points.reduce((lowest, point) => {
if (point[1] > lowest[1]) {
return point;
}
if (point[1] === lowest[1] && point[0] < lowest[0]) {