Skip to content

Instantly share code, notes, and snippets.

View yowchun93's full-sized avatar
🇲🇾
Hello from Malaysia!

YC Yap yowchun93

🇲🇾
Hello from Malaysia!
View GitHub Profile
@yowchun93
yowchun93 / renderProps.tsx
Last active April 6, 2020 13:00
Render Props in Typescript
interface Props {
render: (count: number, setCount: React.Dispatch => JSX.Element
}
// props only render and does nothing else.
export const Counter: React.FC<Props> = ( { render } ) => {
const [count, setCount] = useState(0);
return (
<div>
@yowchun93
yowchun93 / renderProps.jsx
Last active April 5, 2020 05:44
Render Props
// A component with render prop takes a function that returns React element and calls it instead of
// implementing its own render
// render, props is a function
// render prop is a funtion that takes in data, and return JSX element?
<DataProvider render={data => (
<h1>Hello {data.target} </h1>
)}/>
// instead rendering from SharedComponent, you render using props.
@yowchun93
yowchun93 / reactTs.ts
Created April 3, 2020 07:35
React in TS
interface Props {
text: string
}
export const TextField: React.FC<Props> = () => {
}
@yowchun93
yowchun93 / ReturnsPolicy.tsx
Last active April 3, 2020 01:52
ReturnsPolicy
// takes in interface Values
const handleOnSubmit = (values: Values) => {
setIsSubmitting(true);
// this is the one tripping me up.
fetchCurrentShop(async (shop: Shop, address: Address) => {
}
<Formik
initialValues={initialValues}
@yowchun93
yowchun93 / reservations.cs
Last active March 31, 2020 01:03
Restaurant Reservations
public void TryAcceptReturnsReservationIdInHappyPathScenario(
[Frozen]Mock<IReservationsRepository> td,
Reservation reservation,
IReadOnlyCollection<Reservation> reservations,
MaîtreD sut,
int excessCapacity,
int expected)
{
// i think td refers to test-double
// ReservationsRepository is mocked
@yowchun93
yowchun93 / strategy_pattern.rb
Created March 30, 2020 01:30
Strategy Pattern
class TextParser
attr_reader :text, :parser
def initialize(text, parser)
@text = text
@parser = parser
end
def call
parser.call(text)
@yowchun93
yowchun93 / handler.ex
Last active March 29, 2020 00:31
Function clauses
defmodule Servy.Handler do
def route(conv) do
if conv.path == '/wildthings' do
%{ conv | resp_body: "Bears, Lions, Tigers"}
else
%{ conv | r esp_body: "Bears"}
end
end
end
@yowchun93
yowchun93 / handler.ex
Created March 28, 2020 07:19
Elixir Request Router
def Servy.Handler do
def handle(request) do
end
# route GET "/"
def route() do
%{conv | resp_body: "Hello welcome"}
end
end
@yowchun93
yowchun93 / parser.ex
Last active March 28, 2020 07:05
Elixir Request Parser
defmodule Servy.Parser do
def parse(request) do
[request_info, params_string] = String.split(request, "\r\n\r\n")
[request_line | header_lines ] = String.split(request_info, "\r\n")
[method, path, _http_protocol ] = String.split(requestline, " ")
params = parse_params(headers["Content-Type"], params_string)
@yowchun93
yowchun93 / unionFind.java
Last active March 23, 2020 02:01
UnionFind
public static void main(String[] args) {
int n = StdIn.readInt()
QuickFindUF = new QuickFindUF(n);
}
// To merge components containing p and q, change all entries
// id[p] to id[q]
public class QuickFindUF {
private int[] id;
private int count;