Wednesday, November 11, 2015

I like lombok

from
 public final class UserId {
     private final String userId;

    public UserId(String userId) {
        this.userId = userId;
    }

    public String getUserId() {
        return userId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserId userId1 = (UserId) o;
        return !(userId != null ? !userId.equals(userId1.userId) : userId1.userId != null);
    }

    @Override
    public int hashCode() {
        return userId != null ? userId.hashCode() : 0;
    }

    @Override
    public String toString() {
        return "UserId{" + userId + '}';
    }
 }
to
import lombok.Data;

@Data
 public final class UserId {
     private final String userId;
 }
feels almost like scala...

Thursday, June 4, 2015

using ed

Having a moment of spare time I decided to go back to my approach to 99 scala problems. When I started I have used pure scala scripts, so I needed eg. to call them like this:
scala -i p05.scala -i p04.scala -i p03.scala p06.scala
Now I've learnt that it may be easier to go on using
sbt console
However my code was not sbt-friendly, as it caused complaints regarding missing object or class definition. You cannot just define methods like in REPL. So I ended up with urgent neccessity to include my functions into some nice objects. As I am currently playing with rather small machine full IDE like IntelliJ is not an option. So basically I needed to use my command line to:
  • add object definition line before content
  • add closing brace after content
  • where the latter is trivial append to file, usually
    echo "}" >> P01.scala
    
    How to append a line of text at beggining of file? Enter ed.
    Ed is an editor with tradition, used to scarry out younger programmers in team. I have read a fantastic intro to ed not so long ago. This is the ed script for making the changes with line by line explanation what it is doing (note ed will not get this comment):
      
    1                                    //goto first line 
    i                                    //start inserting
    package konopski.skala.ninety.nine { //actual text
      object PP00 {                      //more text
    .                                    //single dote
                                         //ends writing
    $                                    //goto last line
    a                                    //append 
      }                                  //some  
    }                                    //text
    .                                    //ok, that's enough
    
    w                                    //wq seems familiar
    q                                    //have you left any program this way?
    
    After that it took one line in shell I use to complete the task:
      
    for p in p*.scala; cat edme | sed -e s/PP00/$p -e s/\.scala// | ed $p ; end
    
    Note I have used ed's famous cousin - sed to replace placeholder value PP00 to actual file name without extension. That's it, the massive work is done and I have not used copy/paste or even any vim macro. I must say it was amusing equally to playing with scala, however seems to be even more practical than next way to reverse a list ;>

    Wednesday, April 15, 2015

    jq one more time - see only what really matters

    Sometimes you can find lot of information noise. Say I want to see only document id and it's content. Check this:
      
     curl -XPOST "http://es1.grey:9200/settings/d/_search?limit=50" -d'
                             {"query": { "match_all": {}}}' | jq ".hits.hits[] | [ ._id,  ._source ]"
    

    Friday, January 23, 2015

    new favourite command

    A must have for console addicted git users:
    git checkout -
    
    This jumps back and forward between two last branches. Really awesome.

    Thursday, January 8, 2015

    more hints regarding Optionals

    Instead of
            Optional<Camera> camera = getCameraByMac(cameras, mac);        
            if (camera.isPresent()) {            
                return Optional.of( camera.get().getIp() );        
            } else {            
                return Optional.absent();        
            }
    
    
    use
      
       Optional<Camera> camera = getCameraByMac(cameras, mac);
       return camera.transform(getIpFunction);
    
        static final Function<Camera, String> getIpFunction = new Function<Camera, String>() {
            @Nullable @Override
            public String apply(Camera camera) {
                return camera.getIP();
            }
        };
    
    
    instead of
      
            List<Event> result = new LinkedList<>();
            for (Optional<Event> transformedEvent : someEvents) {
                if (transformedEvent.isPresent()) {
                    result.add(transformedEvent.get());
                }
            }
            return result;
    
    
    use
      
            return Optional.presentInstances(events);