Angular wait for multiple Observable

Post-it to this great article: https://coryrylan.com/blog/angular-multiple-http-requests-with-rxjs

Advertisements

Scala – Pattern matching always match first object

In a scala program, I had some code like

def remove(something: Any): Unit = {
  something match {
    case signature => ...
    case tls => ...
    case host => ...
    case port => ...
    case password => ...
    case _ => ...
  }
}

Where signature, tls, host, port, password were all objects defined like

object host

But when calling this method, passing an object, it always matched the first case “signature”.

The solution is to replace the objects in the cases with `object`

def remove(something: Any): Unit = {
  something match {
    case `signature` => ...
    case `tls` => ...
    case `host` => ...
    case `port` => ...
    case `password` => ...
    case _ => ...
  }
}

The solution comes from http://www.scala-lang.org/old/node/5223

Expect script – Read / write file

Thanks to this source : http://wiki.tcl.tk/367

To read data from a file in an Expect script:

#!/usr/bin/expect -f

set fd "input.txt"
set fp [open "$fd" r]
set data [read $fp]

# Read line by line

foreach line $data {

puts "$line\r"

}

And to write data to a file:

#!/usr/bin/expect -f

set outputFilename "output.txt"
set outFileId [open $outputFilename "w"]

puts -nonewline $outFileId "A first line\n"
puts -nonewline $outFileId "A second line\n"

#Close file descriptor to ensure data are flush to file
close $outFileId

Java – HuffmanBTree

An implementation of an Huffman Binary Tree in Java.

The project’s goal was to compress any text given in input to an output file and decompress it.

To compress the input text, our Compress program counts the number of occurences of each letter, put that information in a Priority Queue and then build the Huffman Binary Tree.

The output file is written bit by bit. It first contains the number of chars that will be present in the file. Then there is a representation of the HuffmanBTree so that the file can be decompressed. And finally comes the compressed version of the text.

Here is the HuffmanBTree class. The rest of the code can be found here.

package business;

import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Stack;

import business.exception.EmptyQueueException;

/**
*
* @author Boris
*/
public class HuffmanBTree implements Comparable<HuffmanBTree>{

 private char character;
 private HuffmanBTree left;
 private HuffmanBTree right;
 private HuffmanBTree parent;
 private int freq;
 private boolean isLeaf;
 
 public HuffmanBTree(){
 
 }
 
 //Constructor. Utilis� lorsqu'on cr�e des noeuds externes
 public HuffmanBTree(char c, int f) {
 this.character = c;
 this.freq = f;
 this.isLeaf = true;
 }
 
 //Constructor. Utilis� lorsq'un cr�e des noeuds internes
 public HuffmanBTree(int f,HuffmanBTree left,HuffmanBTree right) {
 this.freq = f;
 this.isLeaf = false;
 this.setLeft(left);
 this.setRight(right);
 }

 public HuffmanBTree merge(HuffmanBTree other) {
 int f = this.getFreq() + other.getFreq();
 HuffmanBTree parent = new HuffmanBTree(f,this,other);
 this.setParent(parent);
 other.setParent(parent);
 return parent;
 }
 
 /**
 * @pre treeList est une priority queue contenant une liste de huffmanBTree orodnn� par fr�quence.
 * @post Tous les arbres pr�sents dans la queue ont �t� fusionn�s en un seul arbre de Huffman, retourn� comme r�sultat.
 * @throws EmptyQueueException si une priority queue est vide.
 */
 public static HuffmanBTree mergeAll(PriorityQueue<HuffmanBTree> treeList) throws EmptyQueueException {
 if (treeList.size()==1){
 return treeList.peek();
 } else if (treeList.size()<1){
 throw new EmptyQueueException();
 } else {
 HuffmanBTree t1 = treeList.poll();
 HuffmanBTree t2 = treeList.poll();
 HuffmanBTree parent = t1.merge(t2);
 treeList.add(parent);
 return mergeAll(treeList);
 }
 }
 
 /**
 * Converti l'arbre en format binaire
 * @return
 */
 public void toBitCode(ArrayList<Boolean> bitList, ArrayList<Character> charList){
 bitList.clear();
 charList.clear();
 
 Stack<HuffmanBTree> toCompress = new Stack<HuffmanBTree>();
 toCompress.push(this);
 
 HuffmanBTree current;
 while(!toCompress.isEmpty()){
 current = toCompress.pop();
 if(current.isLeaf()){
 bitList.add(new Boolean(true));
 charList.add(new Character(current.getChar()));
 } else {
 bitList.add(new Boolean(false));
 toCompress.push(current.getRight());
 toCompress.push(current.getLeft());
 }
 }
 }
 
 //Pour que la file de priorit� ordonne les arbres par ordre de fr�quence
 public int compareTo(HuffmanBTree other) {
 return this.getFreq() - other.getFreq();
 }
 
 //Pour trouver si un arbre a un certain caract�re
 public boolean characterIs(char c){
 return this.character == c;
 }
 
 //utility methods
 public HuffmanBTree getLeft() {return left;}
 public HuffmanBTree getRight() {return right;}
 public HuffmanBTree getParent() {return parent;}
 public void setLeft(HuffmanBTree t) {this.left = t; if(t!=null)t.setParent(this);}
 public void setRight(HuffmanBTree t) {this.right = t; if(t!=null)t.setParent(this);}
 public void setParent(HuffmanBTree t) {this.parent = t;}
 public int getFreq() {return freq;}
 public char getChar(){return character;}
 public boolean isLeaf() {return isLeaf;}
 public void incrementFreq() {this.freq+=1;}
 public boolean hasLeftChild() {return this.left != null;}

 public void setCharacter(char c) {
 this.character = c;
 this.isLeaf = true;
 this.setLeft(null);
 this.setRight(null);
 }
}

Java – BinaryTreeSearch

Implementation of a BinaryTreeSearch in Java

(The project‘s goal was to parse some entries and be able to sort them via filters)

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 *
 * Implémentation de BinaryTreeSearch
 * 
 */
public class BinaryTreeSearch<K, V> {
 private Node root;
 
 private Comparator<K> comparator;
 
 /**
 * Constructeur
 */
 public BinaryTreeSearch(Comparator<K> comparator) {
 this.comparator = comparator;
 }
 
 /**
 * Implémentation de Node
 */
 private class Node {
 private K key;
 private TreeSet<V> values = new TreeSet<V>();
 private Node left;
 private Node right;
 
 /**
 * Constructeur
 */
 public Node(K key, V value) {
 this.key = key;
 this.values.add(value);
 this.left = null;
 this.right = null;
 }
 }
 
 
 /**
 * @pre --
 * @post - retourne null si la clé recherchée k n'est pas dans 
 * l'arbre
 * - retourne un itérateur contenant les valeurs du noeud
 * correspondant à la clé k 
 */
 public Iterator<V> get(K key) {
 return get(root, key);
 }
 /**
 * @pre --
 * @post - retourne null si le noeud est vide
 * - lance une recherche dans l'arbre de gauche si la clé
 * recherchée k est inférieure à la clé de node
 * - lance une recherche dans l'arbre de droite si la clé
 * recherchée k est supérieur à la clé de node
 * - retourne un itérateur contenant les valeurs du noeud
 * si la clé recherchée k est égale à la clé de node
 */
 private Iterator<V> get(Node node, K key) {
 if(node == null) return null;
 int comp = comparator.compare(key, node.key);
 if(comp < 0)
 return get(node.left, key);
 else if(comp > 0)
 return get(node.right, key);
 return node.values.iterator();
 }
 
 /**
 * @pre --
 * @post value est ajouté à l'arbre root
 */
 public void put(K key, V value) {
 root = put(root, key, value);
 }
 
 /**
 * @pre --
 * @post - crée un nouvelle arbre si root est vide
 * - ajoute value à l'arbre root
 */
 private Node put(Node node, K key, V value) {
 if(node == null) return new Node(key, value);
 int comp = comparator.compare(key, node.key);
 if(comp < 0) node.left = put(node.left, key, value);
 else if(comp > 0) node.right = put(node.right, key, value);
 else node.values.add(value);
 return node;
 }
}