Convert Decimal number to Binary in Pharo - smalltalk

I'm trying to Convert a decimal number into binary using Pharo, but I'm having trouble with the recursive message. I figured I could do string concatenation of the values so that when I gave it the value 5 I would get 101 but I'm getting the error which is cryptic. I am adding this to the SmallInteger class. Does anyone have any tips?
errorNotIndexable
"Create an error notification that the receiver is not indexable."
self error: ('Instances of {1} are not indexable' format: {self class name})
decimalBinary
self >= 1
ifTrue: [(self % 2) asStringWithCommas ,
(self // 2) decimalBinary.].
self error: 'Not valid for negative integers'

Try this (Integer>#SmallInteger)
decimalBinary
| b |
b := ''.
self >= 1 ifTrue: [
b := (self % 2) asString, ((self // 2) decimalBinary) asString].
^b
In playgournd
| a |
a := 5.
5 decimalBinary

Related

Rego - assign array to existing array

I'm getting a weird behavior in Rego and I wonder why does it happen.
Link to Rego Playground
When I create an empty array, and than assign to it new array, the count of the first array is still zero:
package play
x[{"msg": msg}]{
c := []
a := [1,2]
b := [3,4]
c = array.concat(a,b)
count(c) > 0
msg := "Length of c is greater than zero"
}
And the output is:
{
"x": []
}
So, I have 2 questions:
Why do I get false in the line count(c)> 0?
How can I assign array to existing one? ( I need it because I have function that returns array and I'm trying to return the concatenation of 2 arrays. e.g.:
func[{"msg": msg}] = c{
a := [1,2]
b := [3,4]
c = array.concat(a,b)
}
Thanks!
Rego values and variables are immutable, so there's no way to assign a new value to an already existing variable. Your example compiles due to using the unification operator (=) rather than the assignment operator (:=).
In the example you provided, simply remove the first assignment:
package play
x[{"msg": msg}]{
a := [1,2]
b := [3,4]
c := array.concat(a,b)
count(c) > 0
msg := "Length of c is greater than zero"
}

how to pass Float in Swift in Dictionary into ObjectiveC method as NSDictionary without turning it into a String?

ObjC method:
- (void)postRequestToURN:(NSString *)URN
parameters:(NSDictionary *)parameters
Swift code calling it:
var paramTest:NSDictionary = NSMutableDictionary()
paramTest.setValue( Int(amount), forKey:"autopayAmt" )
paramTest.setValue( Float(amount), forKey:"autopayAmt1" )
paramTest.setValue( Double(amount), forKey:"autopayAmt2" )
paramTest.setValue( (amount as NSNumber), forKey:"autopayAmt3" )
paramTest.setValue( true, forKey:"autopayAmt4" )
Note: this must be some Swift mapping thing. On Swift 3 as of now. When I put the code with NSDictionary as the parameter then I get this compile error message. This doesn't really make sense as the parameter is an NSDictionary * in the ObjectiveC code.
Cannot convert value of type 'NSDictionary' to expected argument type '[AnyHashable : Any]!'
instead of numbers being in the JSON, I get strings. I need numbers.
{
autopayAmt = 125;
autopayAmt1 = "125.43";
autopayAmt2 = "125.4300003051758";
autopayAmt3 = "125.43";
autopayAmt4 = 1;
}
in the Objective C code the method parameters is passed as:
parameters _TtGCs26_SwiftDeferredNSDictionaryVs11AnyHashableP__ * 0x608000038260 0x0000608000038260
Looks like its goofed up when it goes from the Swift code to the ObjC code. Here's what I print when in ObjectiveC code:
(lldb) po parameters
{
autopayAmt = 125;
autopayAmt1 = "125.43";
autopayAmt2 = "125.4300003051758";
autopayAmt3 = "125.43";
autopayAmt4 = 1;
}
versus if I click up in the stack to the Swift code and print then I see this:
lldb) po paramTest
▿ 5 elements
▿ 0 : 2 elements
- key : autopayAmt
- value : 125
▿ 1 : 2 elements
- key : autopayAmt2
- value : 125.4300003051758
▿ 2 : 2 elements
- key : autopayAmt1
- value : 125.43
▿ 3 : 2 elements
- key : autopayAmt4
- value : 1
▿ 4 : 2 elements
- key : autopayAmt3
- value : 125.43
I just want to know how to pass floats as numbers so they will be sent that way to the JSON server. Data of type Int does output as numbers...

Can a block in Smalltalk be run by itself?

I know blocks can be passed and used in that way.
But is there any way to call a block, just by having a block? Something like this?
aBlock := [ ... ].
aBlock run.
I tried searching for the BlockClosure class in the System browser, but couldn't find it.
Yes! And in fact, you can do pretty interesting things with them, like activating them within themselves. For example, this block computes factorial recursively calling itself:
factorialBlock := [:n |
n <= 1
ifTrue: [n]
ifFalse: [n * (factorialBlock value: n - 1)]
]
and to try it:
factorialBlock value: 5
That would not be an example of a very good coding practice, but it is of the power of block closures!
Let's add that besides value, if your block has arguments, you can also use
[:f | <do something with f>] value: a
where f is a formal argument (an unbound variable name) and a is an actual object that will bind to f when #value: is sent.
For two arguments use value:value: and for many valueWithArguments:, which receives an Array.
Examples
[:x | x squared - 1] value: 3
-> 3 squared - 1 (i.e., 8)
[:x :y | x + 2 / (y - 4)] value: 2 value: 0
-> 2 + 2 / (0 - 4) (i.e., -1)
Exercise
What's the result of evaluating the following?:
block := [:x | x value: 2].
block value: [:y | y + 1]
Yes it can. Also, you send the message 'value' to it to evaluate the block, not 'run'. In fact, you do not have to assign it to a variable. You can just do this:
[ ... ] value.
Check this one:
Lambda Calculus in Pharo
Yes, the Y Combinator is useful in normal programs
https://medium.com/concerning-pharo/lambda-calculus-in-pharo-a4a571869594#.2a78xp31s
From the article:
ycombinator := [ :f |
[ :g | g value: g ] value: [ :g |
f value: [ :x |
(g value: g) value: x ] ] ]
The force is strong in this one.

Creating a Caesar Cipher Method

So I need to get the Caesar Cipher code in smalltalk and create a method and use it so I can do the following test on it
|aString|
aString:=Caesar new encrypt: 'CAESAR'.
Transcript show: aString.
I already have the class made. But I need to make the method of it.
I found this but how can I make a method out of this so I can all the above code in playground.
| i c strCipherText strText iShiftValue iShift |
strText := 'the quick brown fox jumps over the lazy dog'.
iShiftValue := 3.
strCipherText := ''.
iShift := iShiftValue \\ 26.
i := 1.
[ i <= (strText size) ]
whileTrue: [
c := (strText at: i) asUppercase.
( ( c >= $A) & ( c <= $Z ) )
ifTrue: [
((c asciiValue) + iShift > $Z asciiValue)
ifTrue: [
strCipherText := strCipherText, (((c asciiValue) + iShift - 26)
asCharacter asString).
]
ifFalse: [
strCipherText := strCipherText, (((c asciiValue) + iShift)
asCharacter asString).
].
]
ifFalse: [
strCipherText := strCipherText, ' '.
].
i := i + 1.
].
Transcript show: strCipherText.
Transcript cr.
So to make thing clear, I need to make a method using the Caesar Cipher code and use the "aString" code at the beginning and test it with that. I have this code above but this has already text in it and can't be put into the method.
Any help will be appreciated.
As Max said in his comment the code above can be put in a method. The only missing part is a first line with the selector and the formal argument:
caesarCipherOf: strText
<insert the code here>
Another good suggestion by Max is to call the argument aString rather than strText because that's more aligned with how Smalltalkers name things.
But now let's take a look at the source code itself:
The comparison c >= $A & (c <= $Z) means c isLetter.
The conditional calculation of the next character means that we want to shift-rotate c by moving it 3 characters to the right, wrapping it around if it gets beyond $Z. This can be easily expressed as:
(c codePoint - 64 + 3 \\ 26 + 64) asCharacter
where 64 = $A codePoint - 1, is the offset between $A and any given uppercase character c. Note also that I've replaced asciiValue with codePoint.
With these two observations the method can be re-written as
caesarCipherOf: aString
^aString collect: [:c |
c isLetter
ifTrue: [(c asUppercase codePoint - 64 + 3 \\ 26 + 64) asCharacter]
ifFalse: [$ ]]
This is not only shorter, it is more efficient because it avoids creating two new instances of String at every character. Specifically, any expression of the form
string := string , <character> asString
creates two Strings: one as the result of sending #asString, another as the result of sending the concatenation message #,. Instead, #collect: creates only one instance, the one that the method returns.

How to define a single byte variable in go lang

I am a newbie to golang and want to find a way to define a single byte variable.
It's a demo program in Effective Go reference.
package main
import (
"fmt"
)
func unhex(c byte) byte{
switch {
case '0' <= c && c <= '9':
return c - '0'
case 'a' <= c && c <= 'f':
return c - 'a' + 10
case 'A' <= c && c <= 'F':
return c - 'A' + 10
}
return 0
}
func main(){
// It works fine here, as I wrap things with array.
c := []byte{'A'}
fmt.Println(unhex(c[0]))
//c := byte{'A'} **Error** invalid type for composite literal: byte
//fmt.Println(unhex(c))
}
As you see I can wrap a byte with array, things goes fine, but How can I define a single byte without using array? thanks.
In your example, this would work, using the conversion syntax T(x):
c := byte('A')
Conversions are expressions of the form T(x) where T is a type and x is an expression that can be converted to type T.
See this playground example.
cb := byte('A')
fmt.Println(unhex(cb))
Output:
10
If you don't want to use the := syntax, you can still use a var statement, which lets you explicitly specify the type. e.g:
var c byte = 'A'