Skip to content

Instantly share code, notes, and snippets.

View mateuszsokola's full-sized avatar

Matéush mateuszsokola

View GitHub Profile
@mateuszsokola
mateuszsokola / _PureFunctions.md
Last active August 9, 2021 14:36
Pure Functions

Pure functions

A pure function for given the same input will always return the same output and evaluation of this function will not cause any side effects.

Advantages of using pure functions:

  • easy to test, as they always return the same value,
  • easy to debug, as they shouldn't cause any race conditions and circular dependencies,
  • simple and independent, as they don't cause any side effects (it makes design clean),
  • easy to scale application up, as they shouldn't rely on machine state.

Composition

There are two kinds of composition - object composition and function composition:

  • object composition - way of combining simple objects into complex ones.
  • function composition - way of combining simple functions to build more complicated ones.

Why composition is better than inheritance?

It doesn't encourage developers to build large object heritage, so applications aren't complex like enterprise Java apps. We limit the future predition to minimum.

Closures

Closures are scope-bounded bindings.

When can i use them?

  • hide variables
  • hide private functions
  • factories
@mateuszsokola
mateuszsokola / _Higher-Order-Functions.md
Created July 28, 2017 17:56
Higher Order Functions

Higher Order Functions

Higher Order Functions (HOF) take one or more functions as arguments and they must be trigger in the first order. They often return function as the output.

When I use HOF?

  • Defining callbacks
  • Composition
  • map / reduce

Functors

According to wikipedia a functor is a type of mapping between categories arising in category theory. To simplefy it, functors are objects, types, function that contain implementation of the map function. The map transforms contents of the functor.

What needs to be fulfilled?

  • implementation of map function that transforms contents of the functor
  • map always returns functors of the same type

Currying

A curried function takes a single argument and returns another function that takes the next argument until it can evaluate to a result.

Example implementation

Testing with Dependency Injection

DI is a design pattern that allows us to inject dependencies into components. It's about transmiting instances of objects into other objects, which use them (eg. as a parameters). DI makes testing easier as it makes mocking easy. Mocked dependencies can be injected into the tested component.

@mateuszsokola
mateuszsokola / _Testing_with_Jest.md
Last active July 30, 2017 18:14
Testing with Jest

Testing with Jest

Jest framework allows us to set up testing our application without creating complicated configurations. It includes the most of necessary tools needed to have good testing suite such as:

  • mocking - i love that feature
  • jasmine's assertion library
  • code coverage (with reports)
  • easy to plug-in into CI systems
  • based on JSDOM, allows us to test DOM updates without running headless browser.
  • created for React

Keybase proof

I hereby claim:

  • I am mateuszsokola on github.
  • I am msokola (https://keybase.io/msokola) on keybase.
  • I have a public key ASBcN4rSKpKpcGRg_ZC3Npf6eSrLYPEFIcIrCRr7j09KuAo

To claim this, I am signing this object:

import * as React from "react";
import { shallow } from "enzyme";
import Hello from "../Hello";
it("renders the heading", () => {
const result = shallow(<Hello />).contains(<h1>Hello!</h1>);
expect(result).toBeTruthy();
});