Skip to content

Instantly share code, notes, and snippets.

View marcin-chwedczuk's full-sized avatar

Marcin Chwedczuk marcin-chwedczuk

View GitHub Profile
@marcin-chwedczuk
marcin-chwedczuk / compute_e.js
Created September 19, 2016 17:59
Compute 116 000 digits of e using Steve Wozniak algorithm from Byte
// see: https://archive.org/stream/byte-magazine-1981-06/1981_06_BYTE_06-06_Operating_Systems#page/n393/mode/2up
function word(x) { return (x & 0xffff); }
function wordstr(x) { return ('0000000000000000' + x.toString(16)).slice(-4); }
function divide(dividendWords, divisorWord, prevRest) {
var BITS_PER_WORD = 16;
var MAX_DIVISOR = (1 << 16) - 1;
@marcin-chwedczuk
marcin-chwedczuk / Main.java
Created November 15, 2017 19:43
Modify JSON using FastXML Jackson without explicit deserialization
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
ObjectNode rootNode = (ObjectNode)
mapper.readTree(Main.class.getResource("toModify.json"));
ArrayNode listOfObjectsToModify =
(ArrayNode) rootNode.get("listOfObjectsToModify");
@marcin-chwedczuk
marcin-chwedczuk / Main.java
Created November 18, 2017 14:36
Display values of MS-DOS Portable Executable header
// Needs Guava to compile:
//
// compile group: 'com.google.guava', name: 'guava', version: '23.4-jre'
//
import com.google.common.io.LittleEndianDataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
@marcin-chwedczuk
marcin-chwedczuk / swap-tress.js
Created January 14, 2018 10:05
Swap trees.js
function places(treeHeights) {
var N = treeHeights.length;
var H = 0;
for (var i = 1; i < N - 1; i++) {
H += dH(i);
}
for (var firstTree = 0; firstTree < N; firstTree++) {
@marcin-chwedczuk
marcin-chwedczuk / postman.desktop
Created May 3, 2018 14:17
Postman Ubuntu launcher
#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Exec=/home/mc/bin/postman/Postman/Postman
Name=Postman
Comment=Postman
Icon=/home/mc/bin/postman/Postman/resources/app/assets/icon.png
@marcin-chwedczuk
marcin-chwedczuk / eval.fs
Created May 26, 2018 17:50
fsharp-primitive-expression-evaluator
open System
open System.Collections.Generic
open System.Diagnostics
open System.Reflection
open System.Text.RegularExpressions
type Operator =
| Plus | Minus | Multiply | Divide
@marcin-chwedczuk
marcin-chwedczuk / dotnetomaniak-1.md
Created June 8, 2018 19:33
Kiedy unikać async/await w wyrażeniach lambda

Od pewnego czasu do pisania testów jednostkowych używam bibliotek XUnit, NSubstitute oraz NFluent. Zwłaszcza ta ostatnia przypadła mi do gustu.

Jedną z metod oferowanych przez NFluent jest Check.ThatAsyncCode(...), na przykład:

Check
    .ThatAsyncCode(() => Task.Delay(0))
    .DoesNotThrow();
@marcin-chwedczuk
marcin-chwedczuk / csharp_null_and_using_statement.md
Last active December 28, 2018 17:19
C#, null and using statement

Do you know that C# supports using null values with using statements. For example the following program compiles and runs without any problems:

public class Program
{
	public static void Main()
	{
		using(var x = CreateIDisposable()) {
			// prints x = <null>
@marcin-chwedczuk
marcin-chwedczuk / locality_of_configure_await.md
Last active December 30, 2018 10:35
O lokalności `ConfigureAwait`

O lokalności ConfigureAwait

Chciałbym tu naświetlić pewną kwestię związaną z użyciem ConfigureAwait którą wiele osób uczących się programowania asynchronicznego w C# przeocza.

Zacznijmy od prostego przykładu aplikacji WinForms składającej się z jednego przycisku:

public partial class Form1 : Form
{
 public Form1()
@marcin-chwedczuk
marcin-chwedczuk / answer.js
Created May 1, 2019 11:36
Minimal integer n > 0 that for each prime if (p-1) | n then p | n
// Problem:
// Find minimal integer number n > 0 that
// for any prime p:
// if (p - 1) | n then p | n
// where `|` means divide.
//
function gen_primes() {
var N = 1000;