Skip to content

Instantly share code, notes, and snippets.

@julien-h
julien-h / Error handling in C.md
Last active December 19, 2016 19:38
Comparison of different error handling patterns for the same C function

#How to handle errors and resources in C ?

Comparison of different ways to handle errors in the same C function.

The function, called db_create creates a new ImageDatabase instance.

###Table Of Content

  • Snippet 0 duplicates code
  • Snippet 1 uses nested if
@julien-h
julien-h / Taking a picture.md
Last active November 3, 2016 19:11
Small description of what happens when we take a picture

Virtually any computer device in existence today is equipped with at least one digital camera; with over 5 billion users of digital photography, this technology has now fully permeated our society. In this exercise, you will get to build your own image processing pipeline that applies color space transformations to turn the raw data measured by a digital camera into a usable image that can be viewed on a computer screen. In the following, we will explain what this means, and how to implement the associated steps using tools from numerical linear algebra. But first, we shall begin with a brief review of the process that takes place when taking a picture.

A digital camera uses an assembly of optical elements to focus the incident light onto a silicon sensor that consists of millions of tiny regions arranged in a regular grid. The silicon is sensitive to light, and each small region on the sensor (generally referred to as a pixel) measures the portion of light that falls on it.

![Image1](http://rgl.s3.eu-

@julien-h
julien-h / Android: How to design reusable fragment UI.md
Last active December 3, 2016 14:13
Example showing how to design a decoupled fragment UI.

In android development, a fragment is a reusable piece of User Interface. It should contains no business logic and (in the best scenario) should be usable by different apps. Here are some thought about how this can be achieved.

Suppose we want to design a fragment able to display a list of Note with their content, title, date of creation. We want to open the NoteEditor when a note is clicked, and we want to be able to delete one or more notes at once.

capture d ecran 2016-12-03 a 14 36 11

Step 1: design the view

@julien-h
julien-h / Simplify state machines using the composite pattern.md
Last active December 3, 2016 14:54
Sometimes, a class changes its behavior depending on the state it's in. This gist shows how to keep things simple using an application of the composite pattern.

I'm currently working on an android app where we use fragment, reusable piece of UI (user interface). Those fragment become activated when their receive data to display and can transition between multiple states (e.g. waiting data, data displayed, waiting delete response, waiting create response, etc.) depending on user's input and the controller's response.

So they have a messy internal implicit state machine :

void receiveNotes(List<Note> notesToDisplay) {
    if (viewIsCreated) {
 listView.displayData(notesToDisplay);
# MIPS Repl written in MIPS
# Run with MarsIDE (remember to turn on `Settings` —> `Self-modifying code`)
# The input instruction must be converted to decimal
.data
prompt_string: .asciiz "Enter the next instruction to execute: "
.text
main: li $v0 9 # code to request memory
li $a0 16 # enough for 2 instructions + 2 registers ($v0, $a0)
@julien-h
julien-h / Usefull error messages.md
Last active December 19, 2016 20:16
Some thoughts about designing a clean error signaling process that will lead to useful error messages.

I thought a lot about compilers these past few days. One challenge that arises in the front-end is to provide the user with useful error messages: where the error was detected, where it occured, how to fix it, etc.

Don't put it in the middle of the source code

The common pratice in compilers that I know is to use a reporter to signal the error. The reporter is provider with an error string and is expected to display it to the user. So we have user-facing formatted strings in the source code :-(

For instance, this snippet is from llvm's lexer:

@julien-h
julien-h / gaussian_distribution_approximation_graph
Last active March 17, 2018 14:00
Python3 code to show the convergence of a function to the Gaussian distribution using matplotlib animations
#
# Animated graph of two functions whose difference tends to 0 as n -> inf
# Using matplotlib.animation
#
# author: Julien Harbulot
# article url: http://julienharbulot.com/data-science/bernoulli-urn/
# licence: MIT Licence
#
%matplotlib inline
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Math Book Template
% Features:
% - Boxed theorem, definition, etc.
% - Clever references using page number and hyperlink
% - French encoding
%
% Author: Julien HARBULOT
% Licence: MIT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@julien-h
julien-h / Basic scrapping in python
Last active June 30, 2018 23:31
Basic script to scrap a website with python using the standard library. More error checking should go into it.
# -----------------------------------------------------------------------
from urllib.request import Request, urlopen
from urllib.error import URLError
def get_html(url):
# construct an http request for the given url
req = Request(url,
data=None,
headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3)'
@julien-h
julien-h / BeautifulSoup scraping in python3
Created June 11, 2018 07:10
Scrapping with urllib and BeautifulSoup / python3
# First, use URLLIB to fetch HTML files
# -----------------------------------------------------------------------
from urllib.request import Request, urlopen
from urllib.error import URLError
def get_html(url):
# construct an http request for the given url
req = Request(url,
data=None,