Skip to content

Instantly share code, notes, and snippets.

@aakashns
aakashns / RationalGroup.scala
Created April 3, 2015 13:34
pari group for rationsl
object RationalGroup extends Group[(Int, Int)] {
val zero = (0, 0)
def plus(x: (Int, Int), y: (Int, Int)): (Int, Int) = (x, y) match {
case ((x1, x2), (y1, y2)) => (x1*y2 + x2*y1, x2*y2)
}
def inverse(x: (Int, Int)) = (-x._1, x._2)
}
@aakashns
aakashns / Group.scala
Last active August 29, 2015 14:18
Recap of type class from BP1
import annotation.implicitNotFound
@implicitNotFound("No member of type class Group found for type ${T}")
trait Group[T] {
def zero: T
def plus(x: T, y: T): T
def inverse(x: T): T
def minus(x: T, y: T): T = plus(x, inverse(y))
}
@aakashns
aakashns / Name.jsx
Last active October 27, 2015 19:03
A simple controlled component for Name
import React from 'react';
var Name = React.createClass({
getInitialState: function() {
return { value: "" };
},
onValueChange: function(event) {
this.setState({ value: event.target.value });
},
render: function() {
@aakashns
aakashns / Name.jsx
Last active October 28, 2015 09:29
import React from 'react';
var Name = React.createClass({
getInitialState: function() {
return { value: "", errors: [], sync: true };
},
validate: function(value) {
var validations = [
v => v.length < 15 || "Your name is too long!",
@aakashns
aakashns / Name.jsx
Last active October 28, 2015 10:21
Extracting out the common parts from React and React Native
export default function (React) {
var Name = React.createClass({
getInitialState: function() { /* No Change */ },
validate: function(value) { /* No Change */ },
syncToServer: function() { /* No Change */ },
onValueChange: function(value) { /* No Change */ },
render: function() {
var { value, errors, sync } = this.state
var RenderView = this.props.view;
@aakashns
aakashns / Name.jsx
Last active October 28, 2015 09:25
Common component shared between React and React Native
export default function (React) {
var Name = React.createClass({
getInitialState: function() { /* No Change */ },
validate: function(value) { /* No Change */ },
syncToServer: function() { /* No Change */ },
onValueChange: function(value) { /* No Change */ },
render: function() {
var { value, errors, sync } = this.state
var RenderView = this.props.view;
@aakashns
aakashns / NameViewWeb.jsx
Created October 27, 2015 20:26
View for the Web version of Name
import React from 'react';
var NameViewWeb = ({ value, errors, sync, onValueChange }) => {
var errorNodes = errors.map((err, i) => <div key={i}>{err}</div>);
var syncNode = value && <div>{ sync ? "Synced!" : "Syncing..." }</div>;
return (<div>
<label>Name : </label>
<input type="text" value={value}
onChange={e => onValueChange(e.target.value)} />
@aakashns
aakashns / NameWebExample.jsx
Created October 27, 2015 20:34
Example usage of Name on Web
import React from 'react';
import ReactDOM from 'react-dom';
import NameBase from '../shared/Name';
import NameViewWeb from './NameViewWeb';
var Name = NameBase(React);
ReactDOM.render(
<Name view={NameViewWeb} />,
@aakashns
aakashns / NameViewNative.jsx
Last active October 27, 2015 20:52
Native View for Name cross-platform component.
import React from 'react-native';
var {
View,
Text,
TextInput,
StyleSheet
} = React;
var NameViewNative = React.createClass({
@aakashns
aakashns / a.js
Created November 6, 2015 20:20
My gist
console.log('x');