nurkiewicz / LazySeq

Lazy sequences implementation for Java 8
Apache License 2.0
131 stars 32 forks source link

Using with character streams... #7

Closed dmarsh26 closed 10 years ago

dmarsh26 commented 10 years ago

I'm having trouble getting the LazySeq to work, maybe you could help ?

many thanks

package chapter2;

import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.stream.Collectors;

import com.nurkiewicz.lazyseq.LazySeq;

public class Chapter2 {

public static String rnaToAminoAcid(String rna) {

    final Iterator<Character> it = rna.codePoints().mapToObj(x -> Character.valueOf((char)x)).iterator();
    final LazySeq<Character> chars = LazySeq.<Character>of(it);
    LazySeq<List<Character>> s1 = chars.sliding(3);
    LazySeq<String> s2 = s1.map(x -> mapCodon(x));

    s2.forEach(System.out::println);

    s2.collect(Collectors.toList()); // does not work

    s2.collect(Collectors.joining("-")); // does not work

    return "";
}

private static Map<String, String> codonMap = new HashMap<String, String>();

static {
    codonMap.put("UUU", "Phe");
    codonMap.put("UUC", "Phe");
    codonMap.put("UUA", "Phe");
    codonMap.put("UUG", "Phe");
    codonMap.put("UCU", "Ser");
    codonMap.put("UCC", "Ser");
    codonMap.put("UCA", "Ser");
    codonMap.put("UCG", "Ser");
    codonMap.put("UAU", "Tyr");
    codonMap.put("UAC", "Tyr");
    codonMap.put("UAA", "Stop");
    codonMap.put("UAG", "Stop");
    codonMap.put("UGU", "Cys");
    codonMap.put("UGC", "Cys");
    codonMap.put("UGA", "Stop");
    codonMap.put("UGG", "Trp");     
    codonMap.put("CUU", "Leu");
    codonMap.put("CUC", "Leu");
    codonMap.put("CUA", "Leu");
    codonMap.put("CUG", "Leu");
    codonMap.put("CCU", "Pro");
    codonMap.put("CCC", "Pro");
    codonMap.put("CCA", "Pro");
    codonMap.put("CCG", "Pro");
    codonMap.put("CAU", "His");
    codonMap.put("CAC", "His");
    codonMap.put("CAA", "Gln");
    codonMap.put("CAG", "Gln");
    codonMap.put("CGU", "Arg");
    codonMap.put("CGC", "Arg");
    codonMap.put("CGA", "Arg");
    codonMap.put("CGG", "Arg");     
    codonMap.put("AUU", "Ile");
    codonMap.put("AUC", "Ile");
    codonMap.put("AUA", "Ile");
    codonMap.put("AUG", "Met");
    codonMap.put("ACU", "Thr");
    codonMap.put("ACC", "Thr");
    codonMap.put("ACA", "Thr");
    codonMap.put("ACG", "Thr");
    codonMap.put("AAU", "Asn");
    codonMap.put("AAC", "Asn");
    codonMap.put("AAA", "Lys");
    codonMap.put("AAG", "Lys");
    codonMap.put("AGU", "Ser");
    codonMap.put("AGC", "Ser");
    codonMap.put("AGA", "Arg");
    codonMap.put("AGG", "Arg");
    codonMap.put("GUU", "Val");
    codonMap.put("GUC", "Val");
    codonMap.put("GUA", "Val");
    codonMap.put("GUG", "Val");
    codonMap.put("GCU", "Ala");
    codonMap.put("GCC", "Ala");
    codonMap.put("GCA", "Ala");
    codonMap.put("GCG", "Ala");
    codonMap.put("GAU", "Asp");
    codonMap.put("GAC", "Asp");
    codonMap.put("GAA", "Glu");
    codonMap.put("GAG", "Glu");
    codonMap.put("GGU", "Gly");
    codonMap.put("GGC", "Gly");
    codonMap.put("GGA", "Gly");
    codonMap.put("GGG", "Gly");
}

private static String dnaToRna(String data) {
    return data.replaceAll("T", "U");
}

private static String mapCodon(List<Character> codon) {
    return mapCodon(codon.stream().map(x -> x.toString()).collect(Collectors.joining()));
}

private static String mapCodon(String codon) {
    String acid = codonMap.get(codon);
    return acid;
}

}

nurkiewicz commented 10 years ago

LazySeq<T> doesn't provide collect() method because it's not a Stream<T>. However in your case it's not a problem:

s2.stream().collect(Collectors.toList());
//or:
s2.toList();

//and:
s2.stream().collect(Collectors.joining("-"));

Do you have any other issues with this library?

PS: Your code can be simplified slightly:

final Iterator<Character> it = rna.codePoints().mapToObj(x -> (char) x).iterator();
final LazySeq<Character> chars = LazySeq.of(it);
LazySeq<List<Character>> s1 = chars.sliding(3);
LazySeq<String> s2 = s1.map(Chapter2::mapCodon);
dmarsh26 commented 10 years ago

Oh I thought it was a type of stream, many thanks ! great library ! :)

Date: Sun, 12 Oct 2014 11:19:32 -0700 From: notifications@github.com To: LazySeq@noreply.github.com CC: dmarsh26@hotmail.com Subject: Re: [LazySeq] Using with character streams... (#7)

LazySeq doesn't provide collect() method because it's not a Stream. However in your case it's not a problem:

s2.stream().collect(Collectors.toList()); //or: s2.toList();

//and: s2.stream().collect(Collectors.joining("-"));

Do you have any other issues with this library?

PS: Your code can be simplified slightly:

final Iterator it = rna.codePoints().mapToObj(x -> (char) x).iterator(); final LazySeq chars = LazySeq.of(it); LazySeq<List> s1 = chars.sliding(3); LazySeq s2 = s1.map(Chapter2::mapCodon);

— Reply to this email directly or view it on GitHub. =

nurkiewicz commented 10 years ago

Thanks, feel free to bring more question or bugs here.