Facebook feed without ads? Possible?

The cat and mouse game between advertisers and ad-blockers are never over. As soon the blocker guys come up with some solution, the advertisers take counter measure to make the ad-blocking ineffective. No wonder, since selling ads is their business-model.

The gold standard for blocking content -including ads- is to use CSS selector based rules. This is what we can set up in our browser extensions, like Adblock, Adblock Plus, uBlock Origin, etc. All of them come with an extensive set of rules, sometimes referred to as filters.

The weakness in the CSS selector based approach has been identified by advertisers and they have made their counter measure. Since the CSS selector uses fixed IDs and classes, the obvious counter measure is to use dynamically generated IDs and class names on the server side, which changes all the time, making the fixed CSS selector based approach useless.

What else can be done to block unwanted content and ads when dynamic IDs and class names are in place? Well, instead of using CSS selectors, one can inspect the JavaScript objects themselves, using injected JavaScript code. Injecting JavaScript code into pages are supported by the major browsers, through some API. However this often comes with a limitations like the “isolated world” approach like in Chrome, meaning the injected JavaScript code runs in a different -isolated- engine, but shares the same DOM. Of course there are techniques to circumvent this limitation.

Facebook uses React. Even though the IDs and classes are dynamically generated, the properties in the JavaScript objects can be inspected and based on their values one can decide whether a particular feed-unit is genuine from one of your friends, or an ad.

I’ve written a piece of JavaScript code, called as a “scriptlet” -using uBlock Origin’s terminology-, which can be used together with the uBlock Origin browser extension. This small JavaScript code, identifies the ads and other unwanted feed-units -configurable what- and blocks them.

To use it, you first need to install the uBlock Origin browser extension, then configure the extension to use the external scriptlet from my repository, then you need to add a filter line to have it injected.

If you are interested, check out my GitHub repository!

Facebook feed without ads? Possible?

Domain Specific Language Demo in Ruby

Fun with Ruby and Domain Specific Languages

Domain Specific Languages (DSLs) are great, because they enable you to express your ideas in a language which draws it’s vocabulary and structure from the business domain you are concerned with.

DSLs makes it easy to communicate with stakeholders and others with knowledge of the business domain. Also it’s much more natural and easier to solve problems in such a DSL for programmers if available, compared to a general purpose low-level programming language.

DSLs are often split up to external end internal DSLs. In this post I’m trying to show some meta-programming capabilities of Ruby. Here I build a simple internal DSL, on top of Ruby, using Ruby as the host language.

Please note that some sources may refer to internal DSLs as embedded DSLs. Don’t be confused, they both mean the same.

The domain in this post is the building of XML markup from structured DSL code.

Imagine you need to generate XML markup like this:

<person type="Villain" role="President">
  <name>
    <first>Coriolanus</first>
    <last>Snow</last>
  </name>
  <address>
    <street>Presidential Residence</street>
    <city>The Capitol</city>
    <country>Panem</country>
  </address>
  <empty/>
  <withattribsonly key="value" other="other"/>
</person>

Using code-editors and IDEs, it’s not a big deal today, since they aid entering structured data like XML and they can offer tag-closing and autocomplete features and more. Hence you can generate them by entering the XML by hand.

Also, you can opt for writing a small program in a general-purpose programming language to transform some structured data into XML markup, but that assumes that you already have the structured data available to you in some other format. By using a low-level general-purpose programming language you’ll have some performance gain, but coding the tool in that language will be tedious and cumbersome.

A third option could be to use an existing tool/library to perform the transformation for you. With this you could save the time and effort implementing such a tool by yourself.

BUT there’s a forth option, which is the most FUN option in my opinion. Since Ruby is a dynamic, high-level programming language with amazing meta-programming features, it’s so easy to write an internal DSL in Ruby, just to make the XML markup generating happen.

Take a look at the following little DSL for describing structured data:

to_xml do
  person type: 'Villain', role: 'President' do
    name do
      first 'Coriolanus'
      last 'Snow'
    end
    address do
      street 'Presidential Residence'
      city 'The Capitol'
      country 'Panem'
    end
    empty
    withattribsonly key: 'value', other: 'other'
  end
end

If you were looking careful, you probably discovered, that it’s the same structured data as the above in XML markup.

That’s it!

You can find all the source for this little demo below.

# - No explicit support for xml processing instructions
# - No explicit support for comments
# - Lacks support for escaping xml entities
# - No support for a predefined offset to start the indentation form
# - Fixed, non-configurable 2 space indentation
# ...
class SimpleBuilder < BasicObject
  def method_missing(name, *args, &block)
    @level ||= 0
    @target ||= ::STDOUT
    attrs, text = _split_args_to_attrs_and_text args
    if block
      ::Kernel.fail ::ArgumentError, 'Cannot have text content when sub-structure is given' unless text.nil?
      _indent
      _start_tag name, attrs
      _newline
      begin
        @level += 2
        block.call(self)
      ensure
        @level -= 2
        _indent
        _end_tag name
        _newline
      end
    elsif text.nil?
      _indent
      _start_tag name, attrs, true
      _newline
    else
      _indent
      _start_tag name, attrs
      @target << text
      _end_tag name
      _newline
    end
  end

  private

  def _start_tag(name, attrs, close = false)
    @target << "<#{name}"
    attrs.each do |k, v|
      @target << %( #{k}="#{v}")
    end if attrs
    @target << '/' if close
    @target << '>'
  end

  def _end_tag(name)
    @target << "</#{name}>"
  end

  def _newline
    @target << "\n"
  end

  def _indent
    return if @level == 0
    @target << ' ' * @level
  end

  def _split_args_to_attrs_and_text(args)
    attrs, text = nil, nil
    args.each do |arg|
      case arg
      when ::Hash
        attrs ||= {}
        attrs.merge!(arg)
      else
        text ||= ''
        text << arg.to_s
      end
    end
    [attrs, text]
  end
end

def to_xml(&block)
  sb = SimpleBuilder.new
  sb.instance_exec(&block)
end

Please note that this is a simplified solution lacking some important features which could be essential in a real production system -some of them were mentioned in the implementation as comments above-, but these features could be added later and the goal of this demo was only to show how easy it is to write an internal DSL in Ruby.

This code above was based on Builder which is a Ruby Gem available for all of us and is written by Jim Weirich. You may wish to see that too for a more elaborate solution.

References

  1. Embedded DSL
  2. Ruby
  3. Builder by Jim Weirich
Domain Specific Language Demo in Ruby

My Ruby configuration for tinkering

I don’t want the Gem documentation to be built locally on my system, I refer to the online documentation instead.

Setting this up is actually easy.

Put this in a file called .gemrc in your home directory:

gem: --no-document

I like to use the REPL in Ruby (IRB) to tinker with it, and enjoy the dynamic properties of the language.

Unfortunately out of the box IRB lacks such extremely useful tools like code completion, pretty print and automatic indentation.

The interesting thing is that, IRB can be equipped with such tools right out of the box, but it’s not equipped with any by default. Maybe that’s because they didn’t wanted to enforce one particular setting over others, and wanted to leave it up to the user which one to use.

To automate the setup for each and every IRB session, put the following code in a file called .irbrc in your home directory:

require 'irb/completion'
require 'pp'
IRB.conf[:AUTO_INDENT]=true

Let me mention an alternative to IRB with lots of useful tools like the above and more. Pry – An IRB alternative and runtime developer console

My Ruby configuration for tinkering

Ruby is fun!

It’s so easy and natural to solve problems in Ruby!

Consider the problem of finding and printing the second prime number found in the interval 10,000 – 1,000,000.

Side note. One approach can be the usual imperative solution, where the different kinds of computations like enumerating the interval, checking for primality and collecting the solution are interleaving resulting in an effective BUT cryptic solution. It’s much harder to understand -and possibly modify later- the different parts if they are mixed together to form one big monolithic chunk of code. On the other hand one might want to express this idea using the sequence paradigm, where each phase of the computation is done by a function box, these boxes are linked to form a chain, which in turn represents the program. Though this latter approach is very expressive, easy to understand and maintain, BUT it can be very ineffective. Eg. When enumerating big amounts of data, no transition to the next function box is possible until all the data is computed. However there is a clever way to have the best from both worlds. It’s the stream paradigm as shown in the SICP. With this we gain the clarity and elegant style of programming from the sequence paradigm, and the effective computation from the imperative style at the same time.

Fortunately Ruby has lazy enumerators which can support the same idea. In Ruby using lazy enumerators it might look something like:

require 'prime'

primes_in_interval = (10_000..1_000_000).lazy.select(&:prime?)

p primes_in_interval.first(2)[1]

Without the .lazy method call it would filter the whole interval for primes and only when all the primes are filtered and collected into an array, could the printing of the second element be done. It’s a terrible waste. BUT calling the .lazy method on the interval -which returns a lazy Enumerator object- makes a huge difference, since now NO primes are searched and collected, UNTIL they are really needed. With this, only the first two primes are computed, which is a huge gain.

References

Ruby is fun!

Recursive anonymous functions?

Introduction

The Lambda Calculus, invented by Alonzo Church back in the 1930s, can be referred to as the smallest universal programming language. It’s Turing complete, meaning everything which is computable, can be computed in it.

It has only a few basic constructs, like – anonymous – functions and function application. These are the primitives. By adding Church Numerals, representation of Booleans, conditionals, lists and recursion with the help of combinators, we get a fully usable language.

Functional programming was born to Lambda Calculus. Functional programming is getting more and more attention nowadays, because it makes parallel programming so much easier.

Recursion

Recursion is amazing, since it enables us to compute and process self-similar structures in a natural way. It also helps to solve a more complex problem by breaking it down into similar, but simpler problems to be solved separately. Finally recursion can be thought of an alternative to iteration.

Side note. The conventional programming languages are equipped with looping-constructs like, while, for, etc. The interesting thing is that it turns out these are unnecessary, since the same can be accomplished by using functions and function-application. By adding Tail Call Optimisation (TCO for short) and having the underlying system translate them to primitive loops in the machine language, we can achieve the same performance as we were using the usual looping-constructs to express iteration.

To get recursion working, we need to have a “handle” on the function itself. But functions in Lambda Calculus are anonymous. (Meaning, we cannot really refer to them by their names.) So how can we make it work then? Well, we can bind expressions to function parameters. That’s all what’s offered by Lambda Calculus. But as you will see shortly, it’s sufficient to make it work. U and Y combinators to the rescue!

The U combinator is a higher-order function, that applies its parameter to its parameter. It provides a way to get a handle on the function through parameter binding, hence making recursion possible.

Side note. The smallest non-terminating program can be written using the U combinator like this: U(U)

The Y combinator is a bit more complicated compared to the U combinator, and it’s better-known too.

The funny thing is that today’s programming languages, which are functional, or multi-paradigm but supporting the functional paradigm as well, are capable of this kind of programming. That’s because they are descendants of the Lambda Calculus.

Next let’s see how to do recursion with anonymous functions in some mainstream programming languages. And, also in Scheme for those who are not afraid of this beautiful, minimalistic LISP dialect.

Code Examples

Side note. While in Lambda Calculus we bind the name (parameter of the function) to the expression to which the function is applied, they’re bound to the values of the expressions in the following examples, since they use applicative order evaluation. That’s why it’s better to call these combinators applicative order combinators.

function (f) { return f(f); }
(function (f) {
  return f(f);
}(function (h) {
  return function(n) {
    return n <= 1 ? 1 : n * h(h)(n - 1);
};
}))(5);
 -> f { f.call(f) }
-> f { f.call(f) }.call(
  lambda do |h|
    lambda do |n|
      n <= 1 ? 1 : n * h.call(h).call(n - 1)
    end
  end
).call(5)
(lambda (f) (f f))
(((lambda (f) (f f))
  (lambda (h)
    (lambda (n)
      (if (<= n 1)
        1
        (* n ((h h) (- n 1)))))))
  5)

 

References

  1. Lambda Calculus
  2. Anonymous function
  3. Church Encoding, U combinator
  4. Y Combinator
Recursive anonymous functions?

About archiving my SICP related stuff on GitHub

Well, I have done some exercises offered by the books Simply Scheme and the SICP for my own personal development and joy.

One interesting example is the representation of pairs as procedures. Instead of using a data structure we can use ordinary procedures to achieve the same, with identical semantics.

;;; Procedural representation of pairs
(define (cons x y)
  (define (dispatch m)
    (cond ((= m 0) x)
	  ((= m 1) y)
	  (else (error "Only 0 or 1 is allowed as argument -- CONS" m))))
  dispatch)

(define (car z)
  (z 0))

(define (cdr z)
  (z 1))

Another example

;;; Procedural representation of pairs
(define (cons x y)
  (lambda (m) (m x y)))

(define (car z)
  (z (lambda (p q) p)))

(define (cdr z)
  (z (lambda (p q) q)))

Or another example introducing mutation on top of that

(define (cons x y)
  (define (set-x! v) (set! x v))
  (define (set-y! v) (set! y v))
  (define (dispatch m)
    (cond ((eq? m 'car) x)
	  ((eq? m 'cdr) y)
	  ((eq? m 'set-car!) set-x!)
	  ((eq? m 'set-cdr!) set-y!)
	  (else (error "Undefined operation -- CONS" m))))
  dispatch)

(define (car z) (z 'car))

(define (cdr z) (z 'cdr))

(define (set-car! z new-value)
  ((z 'set-car!) new-value)
  z)

(define (set-cdr! z new-value)
  ((z 'set-cdr!) new-value)
  z)

To see more, please take a look at: SICP and Simply Scheme.

Related stuff:

About archiving my SICP related stuff on GitHub

About some of the Big Ideas in CS, based on the SICP

I created a new repo on GitHub, where I plan to collect some cool ideas in CS based on the influential work of Harold Abelson, Gerald J. Sussman with Julie Sussman, the SICP.

My goal is to write about these Big Ideas and also to show them in various programming languages like Scheme, Ruby, JavaScript, etc.

If you understand these concepts, then using them in different programming languages is easy, since you only need to learn the particular notation. (Of course only if the given language supports the particular feature necessary…)

It’s quite amazing, that there are only a few very powerful features required anyways. (Read the SICP to learn about them!)

My GitHub repo about the topic can be accessed here. Check it out!

About some of the Big Ideas in CS, based on the SICP