Skip to content

Instantly share code, notes, and snippets.

@raibima
raibima / tut.py
Last active August 29, 2015 14:17
Tutorial Eva
def occurrences(text1, text2):
result = 0
unique_characters = []
# for every character in text1
for character in text1:
# check whether or not the character has been encountered before
if character not in unique_characters:
# if not, add that character to the list
unique_characters.append(character)
# for every character in text2
@raibima
raibima / injectLogger.js
Last active July 12, 2018 00:58
Simple component setState logger
// Usage
import React from 'react';
injectLogger(React.Component);
// You can also make it specific to just a component
class App extends React.Component {}
injectLogger(App);
// Injecting logger will add additional boolean param to setState.
// Set this to true to perform setState logging
{
"data": {
"authStatus": "AUTHORIZED",
"authMessage": null,
"rescheduleDetail": {
"rescheduleId": "35001186",
"rescheduleType": "RESCHEDULE_FLIGHT_BASIC",
"rescheduleStatus": "ACTIVE",
"notReschedulableReason": null,
"agentRescheduleStatusType": "INFO",
class LRUMap extends Map {
constructor(maxSize = 1) {
super();
this.maxSize = maxSize;
this.insertionOrder = [];
this.set = this.set.bind(this);
}
set(key, value) {
import React from 'react';
import { text } from '@storybook/addon-knobs';
import { storiesOf } from '@storybook/react';
import HelloWorld from './HelloWorld';
const DECORATOR = renderStory => (
<React.Fragment>{renderStory()}</React.Fragment>
);
const { useEffect, useState } = React;
export default function IndexPage(props) {
const [currentHash, setCurrentHash] = useState(window.location.hash);
useEffect(() => {
function handleHashChange(e) {
setCurrentHash(window.location.hash);
}
window.addEventListener('hashchange', handleHashChange);
/**
* Function component with Hooks
* @see: https://reactjs.org/docs/hooks-intro.html
*/
import { useRouter } from "next/router";
function FunctionComponentPage() {
const router = useRouter();
return <h1>{`ID: ${router.query.id}`}</h1>;
}
function createService<F extends (...args: any[]) => Promise<any>>(fn: F) {
type Result = PromiseType<ReturnType<typeof fn>>;
type Entry = Result | Promise<void> | Error;
// const cache = new LRU<string, Entry>({maxSize: 5});
const cache = new Map();
return {
call(...args: Parameters<typeof fn>): Result {
const key = JSON.stringify(args);
const hit = cache.get(key);
if (!hit) {
@raibima
raibima / generator-example.jsx
Created January 21, 2020 08:58
Generator Example 1
import { range, map } from './utils.js';
let iter = range(0, 1000); // almost-zero memory & computation overhead!
let list = (
<ul>
{map(iter, n => <li key={n}>{n}</li>)
</ul>
);
module.exports = function (babel) {
const { types: t } = babel;
let counter = 0;
const specialProps = {
padding: 1,
borderRadius: 1
};
let styles = null;