Merge: Better autoinit conflict message
authorJean Privat <jean@pryen.org>
Tue, 14 Jul 2015 19:50:57 +0000 (15:50 -0400)
committerJean Privat <jean@pryen.org>
Tue, 14 Jul 2015 19:50:57 +0000 (15:50 -0400)
On

~~~nit
class A
    var x: Int
end

class B
    var y: Bool
end

class C
    super A
    super B
end
~~~

The error message

> a.nit:9,7: Error: conflict for inherited inits a#B#init(y=) and a#A#init(x=)

now become

> a.nit:9,7: Error: cannot generate automatic init for class C. Conflict in the order in inherited initializers a#B#init(y=) and a#A#init(x=). Use `autoinit` to order initializers. eg `autoinit x=, y=`

(fell free to simplify or propose better)

Close #1549

Pull-Request: #1568
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>

examples/rosettacode/entropy.nit [new file with mode: 0644]
examples/rosettacode/entropy_narcissist.nit [new file with mode: 0644]
examples/rosettacode/fibonacci_word.nit [new file with mode: 0644]
lib/counter.nit
lib/standard/bytes.nit
tests/niti.skip
tests/nitvm.skip
tests/sav/entropy.res [new file with mode: 0644]
tests/sav/entropy_narcissist.res [new file with mode: 0644]
tests/sav/fibonacci_word.res [new file with mode: 0644]
tests/sav/nitpick_args1.res

diff --git a/examples/rosettacode/entropy.nit b/examples/rosettacode/entropy.nit
new file mode 100644 (file)
index 0000000..f47910c
--- /dev/null
@@ -0,0 +1,32 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Entropy
+# SEE: <http://rosettacode.org/wiki/Entropy>
+module entropy
+
+import counter
+
+# Basic implementation with a hashmap of chars to count them
+fun entropy(string: String): Float
+do
+       var cpt = new HashMap[Char, Int]
+       for char in string.chars do
+               var occ = cpt.get_or_default(char, 0)
+               cpt[char] = occ + 1
+       end
+
+       var len = string.length.to_f
+       var e = 0.0
+       for char, occ in cpt do
+               var freq = occ.to_f / len
+               e = e - freq * freq.log_base(2.0)
+       end
+       return e
+end
+print entropy("1223334444")
+
+# Alternative one-liner implementation using the `Counter::entropy` method of the library `counter`
+print "1223334444".chars.to_counter.entropy
diff --git a/examples/rosettacode/entropy_narcissist.nit b/examples/rosettacode/entropy_narcissist.nit
new file mode 100644 (file)
index 0000000..28b6212
--- /dev/null
@@ -0,0 +1,13 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Entropy/Narcissist
+# SEE: <http://rosettacode.org/wiki/Entropy/Narcissist>
+module entropy_narcissist
+
+import counter
+
+# Should be run in the right directory
+print "entropy_narcissist.nit".to_path.read_all.chars.to_counter.entropy
diff --git a/examples/rosettacode/fibonacci_word.nit b/examples/rosettacode/fibonacci_word.nit
new file mode 100644 (file)
index 0000000..fec4929
--- /dev/null
@@ -0,0 +1,28 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Fibonacci_word
+# SEE: <http://rosettacode.org/wiki/Fibonacci_word>
+module fibonacci_word
+
+import counter
+
+var words = new Array[String]
+words[0] = ""
+words[1] = "1"
+words[2] = "0"
+
+for i in [1..37] do
+       var w
+       if i >= words.length then
+               w = words[i-1] + words[i-2]
+               words[i] = w
+       else
+               w = words[i]
+       end
+       var out = w
+       if w.length > 40 then out = "..."
+       print "{i}\t{w.length}\t{w.chars.to_counter.entropy.to_precision(16)}\t{out}"
+end
index befce9c..644c115 100644 (file)
@@ -254,6 +254,36 @@ class Counter[E]
                end
                return (sum / map.length.to_f).sqrt
        end
+
+       # The information entropy (Shannon entropy) of the elements in the counter (in bits).
+       fun entropy: Float
+       do
+               var res = 0.0
+               var sum = self.sum.to_f
+               for k, v in self do
+                       var f = v.to_f / sum
+                       res = res - f * f.log_base(2.0)
+               end
+               return res
+       end
+end
+
+redef class Collection[E]
+       # Create and fill up a counter with the elements of `self.
+       #
+       # ~~~
+       # var cpt = "abaa".chars.to_counter
+       # assert cpt['a'] == 3
+       # assert cpt['b'] == 1
+       # assert cpt.length == 2
+       # assert cpt.sum == 4
+       # ~~~
+       fun to_counter: Counter[E]
+       do
+               var res = new Counter[E]
+               res.inc_all(self)
+               return res
+       end
 end
 
 private class CounterComparator[E]
index 2a3526f..9843ddc 100644 (file)
@@ -46,6 +46,7 @@ class Bytes
                init(ns, 0, 0)
        end
 
+       # Init a `Bytes` with capacity `cap`
        init with_capacity(cap: Int) do
                var ns = new NativeString(cap)
                init(ns, 0, cap)
@@ -96,6 +97,16 @@ class Bytes
                end
        end
 
+       #     var b = new Bytes.empty
+       #     b.append([0x41u8, 0x41u8, 0x18u8])
+       #     b.pop
+       #     assert b.to_s == "AA"
+       redef fun pop do
+               assert length >= 1
+               length -= 1
+               return items[length]
+       end
+
        redef fun clear do length = 0
 
        # Regenerates the buffer, necessary when it was persisted
@@ -159,6 +170,7 @@ private class BytesIterator
 end
 
 redef class NativeString
+       # Creates a new `Bytes` object from `self` with `strlen` as length
        fun to_bytes: Bytes do
                var len = cstring_length
                return new Bytes(self, len, len)
index 0da465e..5c4ff8a 100644 (file)
@@ -27,3 +27,4 @@ mpi_simple
 blink
 input
 first_letter_last_letter
+fibonacci_word
index 0da465e..5c4ff8a 100644 (file)
@@ -27,3 +27,4 @@ mpi_simple
 blink
 input
 first_letter_last_letter
+fibonacci_word
diff --git a/tests/sav/entropy.res b/tests/sav/entropy.res
new file mode 100644 (file)
index 0000000..29f9066
--- /dev/null
@@ -0,0 +1,2 @@
+1.846
+1.846
diff --git a/tests/sav/entropy_narcissist.res b/tests/sav/entropy_narcissist.res
new file mode 100644 (file)
index 0000000..ba66466
--- /dev/null
@@ -0,0 +1 @@
+0.0
diff --git a/tests/sav/fibonacci_word.res b/tests/sav/fibonacci_word.res
new file mode 100644 (file)
index 0000000..38a9498
--- /dev/null
@@ -0,0 +1,37 @@
+1      1       0.0000000000000000      1
+2      1       0.0000000000000000      0
+3      2       1.0000000000000000      01
+4      3       0.9182958340544894      010
+5      5       0.9709505944546686      01001
+6      8       0.9544340029249650      01001010
+7      13      0.9612366047228760      0100101001001
+8      21      0.9587118829771316      010010100100101001010
+9      34      0.9596868937742170      0100101001001010010100100101001001
+10     55      0.9593160320543778      ...
+11     89      0.9594579158386696      ...
+12     144     0.9594037542210228      ...
+13     233     0.9594244469559864      ...
+14     377     0.9594165437404406      ...
+15     610     0.9594195626031440      ...
+16     987     0.9594184095152248      ...
+17     1597    0.9594188499578102      ...
+18     2584    0.9594186817240320      ...
+19     4181    0.9594187459836638      ...
+20     6765    0.9594187214386752      ...
+21     10946   0.9594187308140276      ...
+22     17711   0.9594187272329618      ...
+23     28657   0.9594187286008076      ...
+24     46368   0.9594187280783368      ...
+25     75025   0.9594187282779028      ...
+26     121393  0.9594187282016752      ...
+27     196418  0.9594187282307916      ...
+28     317811  0.9594187282196702      ...
+29     514229  0.9594187282239182      ...
+30     832040  0.9594187282222962      ...
+31     1346269 0.9594187282229156      ...
+32     2178309 0.9594187282226794      ...
+33     3524578 0.9594187282227690      ...
+34     5702887 0.9594187282227344      ...
+35     9227465 0.9594187282227478      ...
+36     14930352        0.9594187282227430      ...
+37     24157817        0.9594187282227448      ...
index 6e97e37..ba7c7ea 100644 (file)
@@ -1,5 +1,3 @@
-../lib/standard/bytes.nit:49,7--19: Documentation warning: Undocumented property `with_capacity`
-../lib/standard/bytes.nit:162,6--13: Documentation warning: Undocumented property `to_bytes`
 ../lib/standard/stream.nit:426,6--17: Documentation warning: Undocumented property `buffer_reset`
 ../lib/standard/file.nit:455,6--19: Documentation warning: Undocumented property `read_all_bytes`
 test_advice_repeated_types.nit:36,15--20: Warning: useless type repetition on redefined attribute `_a`