Skip to content. | Skip to navigation

Personal tools
Log in
Sections
You are here: Home

jorgenmodin.net - Blog

How to do a diff on a word/character basis rather than on a line basis

Posted by admin |

How to do a diff on a word/character basis rather than on a line basis

Use git's diff like so:

git diff --word-diff=color --word-diff-regex=. file1 file2

 

From command line - Using 'diff' (or anything else) to get character-level diff between text files - Stack Overflow

Sep 13, 2017 02:28

Starting to understand React.js

Posted by admin |

I've stared at React.s js a couple of times failing to understand it, but this time I think I've got it. Info in this post may change and will be expanded.

So, let's look at one of the code examples of React.js:

class Timer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {secondsElapsed: 0};
  }

  tick() {
    this.setState((prevState) => ({
      secondsElapsed: prevState.secondsElapsed + 1
    }));
  }

  componentDidMount() {
    this.interval = setInterval(() => this.tick(), 1000);
  }

  componentWillUnmount() {
    clearInterval(this.interval);
  }

  render() {
    return (
      <div>Seconds Elapsed: {this.state.secondsElapsed}</div>
    );
  }
}

ReactDOM.render(<Timer />, mountNode);

What we're dealing with above is a class definition. It can be instantiated as an object with this HTML-like construct:

ReactDOM.render(<Timer />, mountNode);

That is not proper javascript but something called JSX: Introducing JSX - React.

This snippet  instantiates the Timer class into an object and attaches the object to a DOM. As can be seen, no quotes are needed around the angle brackets. It could have had initialization parameters, in which case it would look like this:

ReactDOM.render(<Timer foo_"baz" bar="garbl"/>, mountNode);

Looking back at the class definition, we can see that methods are just written as barewords with a trailing bracket for method arguments, followed by curly brackets for the method body. "this" refers to what would be called "self" in e.g. python. There is no "def", "function or "fun" preceding the method name. Ok, looking closer this is actually ES 6 classes: Classes - JavaScript | MDN.

  componentWillUnmount() {
    clearInterval(this.interval);
  }

 

A number of methods are special:

Object methods

  • render()
  • constructor(props) - initializes the object on object creation
  • componentWillMount() - event handler
  • componentDidMount() - event handler
  • componentWillReceiveProps(nextProps) - event handler
  • shouldComponentUpdate(nextProps, nextState) - status handler
  • componentWillUpdate(nextProps, nextState) - event handler
  • componentDidUpdate(prevProps, prevState) - event handler
  • componentWillUnmount() - event handler
  • setState(updater, [callback]) - mutator, the updater argument is a function, the callback will be executed after the new state has de facto been set, which may happen asynchronously
  • component.forceUpdate(callback)


Class properties

  • defaultProps
  • displayName


Instance Properties

  • props
  • state

For reference, see here: React.Component - React

If we look at the code example where setState is involved:

  tick() {
    this.setState((prevState) => ({
      secondsElapsed: prevState.secondsElapsed + 1
    }));
  }

It may first look like prevState is an object being passed into this.setState and where would that come from? But it isn't, because that would be:

this.setState(prevState)

...instead what we are looking at is passing in an anonymous function to setState:

    this.setState((prevState) => ({
      secondsElapsed: prevState.secondsElapsed + 1
    }));

...where prevState is simply a placeholder for the first argument that the anonymous function will be called with. In fact you could have it as:

  tick() {
    this.setState((foo) => ({
      secondsElapsed: foo.secondsElapsed + 1
    }));
  }

 

 

Sep 12, 2017 12:10

Timothy Snyder: Europe, its past and what is its future?

Posted by admin |

Snyder points out at the end that even if the EU is a great answer to problems of European history, it has not been that articulate about its future Timothy Snyder - Nations, Empires, Unions: European Integration and Disintegration Since 1914 - YouTube

This comes at the end, the whole talk which is from Oslo in 2014, is a great journey through the topic as defined in the talk's title.

Sep 10, 2017 01:02

Ubuntu 17.04 crashing hard - disable Chrome hardware acceleration

Posted by admin |

Today my Ubuntu 17.04 crashed hard a couple of times. Even the cursor wouldn't move. After having restarted (again) I used journalctl on the command line, to view what were the messages from the previous boot.

journalctl -b -1

(You need to have journalctl configured to save log messages from previous boots, for this to work)

The very last message was in red and said:

RIP: gen8_ppgtt_alloc_page_directories.isra.38+0x115/0x250 [i915] RSP: ffffb88ac3aab880

I'm not initimately familar with the log severity codes, but RIP sounds likes it's Rest In Peace which might be a bit late in the chain for diagnostics. But there was a message also in red a bit higher up that began with:

kernel: BUG: unable to handle kernel NULL pointer dereference at 0000000000000018
kernel: IP: gen8_ppgtt_alloc_page_directories.isra.38+0x115/0x250 [i915]

I googled that and found some ideas, one being to disable hardware acceleration in Google Chrome, since that seemed to trigger the bug. It seems to work!

See: Bug #1680904 “zesty unable to handle kernel NULL pointer derefer...” : Bugs : linux package : Ubuntu

Aug 20, 2017 05:50

How the extreme left turned to race in order to get rid of the center of politics

Posted by admin |

The left has always had a problem demarcating to the extreme left. Although they all agree that communism was bad, they seem to have this lingering suspicion that maybe it was good, but just had a bit of bad luck. See for example the recent spate of articles in the New York Times (one example).

Sometimes the leftists' often reasonable actions and views, get hijacked by the extreme left and get tweaked into insanity, and then beamed back at the left.

But the left never, not even the extreme left, ever went for racism. Socialism and even communism is inherently anti-racist. In fact commies see all human individuals as replaceable, so race doesn't make sense. The left could argue for women, for workers, but it would not argue for race. After all the Soviet Union defeated the racist nazis, and although the Soviet killed massive amounts of their own people, it wasn't officially at least based on the principle that you were killed for your perceived race (I write "perceived" because it wasn't up to you to decide what or who you were; it was foisted upon you.).

However as time has gone by and no revolution in the West has turned up, the extreme leftists came to the conclusion that by focusing on race - the taboo subject - they could very well destabilize society in a quicker and more insidious way. Once the chaos arrives, they reason, they can take power and stamp out that pesky race thing which had then served its purpose. But in the history of communism and chaos, it seems to be a bit of a crap shoot: Sometimes the extreme right instead come out as winners, and go on to destroy civilization and society in their particular way.

By embracing race as a divider, the extreme left hopes to nurture the neo nazis and finally get rid of that pesky middle of the political field. They mimic their enemy, and the epic, final standoff should materialize. Although negative rights are important when working against racism, positive rights can be quite dangerous.

Part of the extreme left's success rests on the latent racism present in our societies. The extreme right most certainly do not need the extreme left to flourish it seems, but it does not mind being cast into a nemesis role. This is a war on the political center, coordinated between communists (turned race baiters) and neo nazis.

The solution is to be resistant to thinking in terms of race. Whenever you're tempted to think in terms of race, think in terms of marginalization and lack of opportunities for individuals, not collectives of a certain race, and watch out for discrimination, racist or otherwise. Resist racism when you see it and call it out. And focus on supplying opportunities and justice, i.e. freedom.

Race is bullshit, individuals are not.

 

Aug 19, 2017 04:50

Accidentally social entrepreneur

Posted by admin |

Photo from a recent party of ours, © Mikael Ståldal

To be a true entrepreneur, there should be a legacy, something that continues after you quit. It seems I made it, in the unexpected field of sobriety, joy and dancing, of all things.

Five years ago we started monthly sober Saturday night dancing parties in Stockholm. Two weeks ago, after about 60 parties averaging around 150 jolly dancers each, I called it quits.

But the Men Dansa party machine will go on without me. It's cooperative, value driven and supported by many. Here is my thank you note on Facebook:

 

And come to think of it. I do not own Facebook, but I do own my blog. so here comes a copy of the message in text format:

After five years of Men Dansa parties I have decided to step down and let Men Dansa continue with its current strong lineup of board members, organizers and volunteers. 

And remember, due to the sheer number of motivated people involved, the parties will continue to be just as awesome :)

I took the initiative to and then co-founded Men Dansa with Janne Pettersson and Mikael Ståldal. This is more than five years ago now. We started the Men Dansa as a non profit organisation and decided that Men Dansa should be built on voluntary work, including ourselves: The only one who gets paid is the accountant. If we get a surplus at the end of the year, we donate it. So far it has been to “Cancerfonden” each year. 

Through the years hundreds of volunteers, dozens of organizers and teachers and DJs, and a bunch of board members have helped making the Men Dansa parties the most open, cozy and welcoming Latin dance parties in Stockholm ( in my humble opinion :) ).

We wanted to get new people into the fantastic Latin dance culture where alcohol can not only be absent, but is truly irrelevant, and where drunkenness – given the complexity of the dances we offer – becomes impossible. We wanted to give a solid alcohol free Saturday night alternative to hanging out at drinking places.

Me stepping down is not due to any conflict, it’s rather that I think that we have a really strong lineup of organizers and members of the board, so I feel that I can leave and the show will go on. This will be a very managed handover. In fact I have tried to wind down my participation for some time!

There are so many people I would like to thank: The board and organizers warrant a special mention. You guys – past and present – are ultimately the guys that define the organization. In my experience the volunteers at these passionate dance parties, the organizers, dance teachers, DJs, and all the people who have handled the door & the food – in all we are talking of hundreds of people – are some of the nicest people I’ve met. I’d like to thank you all for having created such great parties together with me and all our guests!!

Aug 17, 2017 01:10

Internet (som det används nu) balkaniserar Sverige

Posted by admin |

Internet kan användas på olika sätt. Man kan kommunicera med människor med tusentals olika åsikter och läsa texter med lika många olika perspektiv.

Men ett annat sätt använda Internet är att kommunicera med dem som tycker likadant som en själv. Och det är där de flesta av oss hamnar. Det är ju inte precis så att man letar upp folk man inte gillar och blir Facebook-vänner med dem.

Detta gör att man smygande börjar få en lite felaktig bild av världen. Man tror att folk är mer som man själv än vad de egentligen är. Man snackar ihop sig. Och man känner mindre behov av att prata med och förstå andra människor bara för att de finns i ens fysiska närhet. Ju mer ihopsnackad man blir med sina vänner och bekantskaper, desto konstigare framstår "de andra".

Men dessa "andra" människor kommer att fortsätta att finnas runt omkring en. Och när deras åsikter och ens egna åsikter blir alltför annorlunda, när var och en seglar ut i sitt eget sociala nätverk, då smäller det förr eller senare mellan dessa grupper.

Förut hade vi TV och tidningar. Dessa gav ofta en felaktig bild, men det var en bild som vi alla delade.

Strax efter klockan 20 igår gick jag och en kompis vid Slussen i Stockholm. Ett halvdussin polisbilar med påslagna sirener for förbi oss. Senare förstod vi att de ingripit vid en attack mot afghaner på Mynttorget. Dessa afghaner protesterar mot beslut att de ska skickas tillbaka till Afghanistan. När de fredligt protesterar mot detta beslut blir de attackerade av nynazister.

Och det slog mig ikväll, hur når man dessa nynazister? Och jag insåg att det kan bli svårt. De har säkert sina egna sociala nätverk, sina egna texter och det gör att de kan bli ännu mer extrema. För det som är extremt börjar uppfattas som normalt. För tjugo år sedan hörde jag en märklig konversation bland några unga människor som gick förbi mig, den gick så här:

-Vi tog taxi hem

-Men blir inte det väldigt dyrt?

- Nä vi gasa han [tårgas, min anmärkning].

- Vem?

- Taxichauffören

- Varför det?

- Det är billigare.

Och jag tänkte att detta var personer för vilka ett kriminellt beteende var normaliserat. De hade egna normer. Men på den tiden, för 25 år sedan var de ändå en liten kriminell grupp vars normer och världsbild snart skulle komma att slås sönder när de splittras och sugs upp i större sociala nätverk. Men idag kan väldigt avvikande normer hållas levande via våra bubblor på Internet.

Jag tror följande behöver göras:

  • Vi måste ha en ordningsmakt som är en självklar del att se i vardagen. Vi måste ha vänliga patrullerande poliser, i princip allestädes närvarande. När människor kan segla ut i fantasterier på Internet om hur världen fungerar, behövs det några ute i den fysiska världen som förankrar dem i att bete sig som folk.
  • Vi vanliga människor måste ta tag i folk och prata med folk som vi normalt sett inte umgås med. Att gå runt utan att behöva växla några ord med andra, är i och för sig många Stockholmares våta dröm (och kanske även i andra delar av landet), men man kan inte ignorera den fysiska verklighet man lever i. Det är den fysiska verkligheten som kan bli farlig för dej, inte text och bilder på Internet från dina vänner. Ja det kommer att bli ytligt, det blir ansträngda leenden och ytliga sociala aktiviteter tillsammans med folk man knappt känner. Men det måste nog till. Vi måste umgås mer.
Aug 11, 2017 11:19

Kritik, från bäst till sämst metod

Posted by admin |

Det finns en skala vad gäller kvalitet i debatt: Att säga att någon har fel i sak, att säga att någon är inkompetent, att säga att nån är dum i huvudet, och att säga att någon är köpt eller manipulerar för egen vinning.

Kritik, från bäst till sämst metod.
Säga att nån:

  1. Har fel i sak
  2. Är inkompetent
  3. Är dum i huvet
  4. Är köpt eller manipulerar för egen vinning
Aug 11, 2017 01:23

How to install TFHE homomorphic encryption & run the tutorial

Posted by admin |

How to install the TFHE Fast Fully Homomorphic Encryption over the Torus library and run its tutorial on Linux.

Make sure you have the dependencies, for ubuntu it is:

sudo apt-get install build-essential cmake

Download the tfhe source tarball, extract it and enter the top directory

mkdir build
cd build

Change "/home/jorgen/usr/local" to where you want your local install below:

cmake ../src -DCMAKE_INSTALL_PREFIX=/home/jorgen/usr/local
make
make install

Download the example files from

https://tfhe.github.io/tfhe/assets/data/samples.tar.gz

Make sure you have correct paths by making a script called "initialize.sh" with the following content:

#configure environment variables
TFHE_PREFIX=/home/jorgen/usr/local #the prefix where you installed tfhe
export LIBRARY_PATH=$LIBRARY_PATH:$TFHE_PREFIX/lib
export C_INCLUDE_PATH=$C_INCLUDE_PATH:$TFHE_PREFIX/include
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:$TFHE_PREFIX/include

Run it from the terminal you are in with:

. initialize.sh

Enter the directory where the tutorial files are

Make a shell script called "run.sh" with the following contents:

g++ -o alice alice.c -ltfhe-spqlios-fma
g++ -o cloud cloud.c -ltfhe-spqlios-fma
g++ -o verif verif.c -ltfhe-spqlios-fma

chmod 755 alice
chmod 755 cloud
chmod 755 verif

./alice
./cloud
./verif

Run it

. run.sh
Aug 06, 2017 02:15

How to move the close button to the right in Gnome (Ubuntu 17.04)

Posted by admin |

Tested by me:

gsettings set org.gnome.desktop.wm.preferences button-layout 'minimize,maximize:close'
Jun 30, 2017 12:46