Skip to content

Instantly share code, notes, and snippets.

@leksak
leksak / read-file-util-method.java
Last active November 23, 2015 09:39
Read an entire file into memory
/**
* Lossless reading of a file (line-separators are preserved).
* May temporarily require memory several times the size of the
* file. For a short time the raw file contents (a byte array),
* and the decoded characters reside in memory simultaneously.
* It is safest to apply to files that you know to be small
* relative to the available memory.
* @param path the absolute path of the file.
* @param encoding the charset with which the bytes in the file
* should be decoded.
@leksak
leksak / algae1.hs
Last active December 21, 2015 22:02
L-system
{-# LANGUAGE MultiWayIf #-}
data Algae = A | B deriving (Show, Eq)
algae :: [Algae] -> [Algae]
algae = concatMap (\c -> if
| c == A -> A:[B]
| c == B -> [A])
@leksak
leksak / pythagoras.hs
Last active December 21, 2015 22:01
L-system
{-# LANGUAGE MultiWayIf #-}
data Variables = Zero | One | LB | RB deriving (Show, Eq)
-- LB and RB symbolises left and right brackets, i.e. [, ]
pythagoras = concatMap (\c -> if
| c == Zero -> One : [LB, Zero, RB, Zero]
| c == One -> One : [One]
| c == LB -> [LB]
| c == RB -> [RB])

Q: What are the differences between a circuit switching network and a packet switching network?

Answer

Bandwidth

A circuit switching network has a guaranteed bandwidth, since the entire established circuit is dedicated to the communication between the two parties. The disadvantage of this is that the resource (the bandwidth) is not guaranteed to be fully utilised

The most common functions in a protocol should handle

  • The dialogue, who gets to send in which order
  • The addressing, how to broadcast one's identity and the identity of one's intended recipient.
  • Frame-format, how information (data) is packaged (framed)
  • Error-handling
@leksak
leksak / internet-protocol-stack-overview.md
Last active December 6, 2015 19:22
Answer to exam question regarding what roles each layer in the IP-stack has

The different layers that make up the internet protocol stack are

  • Application
  • Transport
  • Network
  • Link
  • Physical

@leksak
leksak / layered-architecture-exam-151030-Q1.md
Created November 25, 2015 18:54
Proposed solution to an exam question

Computer communication and computer networking (Datakommunikation och datornät, DOD)

Exam 2015-10-30. Question 1.

Within computer networking one often speaks of a layered architecture.

  • Question 1.1 What does one mean by this term, i.e. layered architecture?

Answer: With a layered architecture one is referring to a system that is modularized in such a way that the system is composed in layers where each layer provides a

@leksak
leksak / HeterogeneousContainer.java
Created November 26, 2015 13:08
Found in my old source-code
/**
* Provides the necessary interface for a typesafe heterogeneous
* container, where the key and the return type is parametrized.
*
* A parametrized key is used to insert and retrieve values. By
* utilising the generic type system the type of the value returned is
* guaranteed to be the same as its key.
*
* The parametrized return value is to allow for a facsimile to a
* builder pattern, leading to brevity of expression for adding
@leksak
leksak / decimal_expansion_difference.m
Created December 21, 2015 09:42
Inspect differing digits in decimal expansion
function [n, difference] = decimal_expansion_difference(target, actual, pertinent_digits)
%DECIMAL_EXPANSION_DIFFERENCE Inspect differing digits in decimal expansion
%
% INPUT:
% target the target value.
% actual the computed value, its distance must be < 1 away from target.
% If violated you will incur an assertion error.
% pertinent_digits an upper bound for beyond which we stop caring about the amount
% of differing digits in the decimal expansion.
%
@leksak
leksak / l-system.hs
Created December 21, 2015 22:29
Represent arbitrary D0Ls
data D0L m a = D0L { axiom :: m a
, rules :: a -> m a
}
appliedTo :: Monad m => (a -> m b) -> m a -> m b
appliedTo = flip (>>=)
eval :: Monad m => D0L m a -> D0L m a
eval (D0L axiom rules) = D0L (rules `appliedTo` axiom) rules