F# stop console from closing [duplicate] - visual-studio-2010

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
F#: This expression should have type 'unit', but has type 'ConsoleKeyInfo'
I'm learning F# so i wrote a 3 lines of code in VS2010 and I want to see the result but the console closes. System.Console.Read System.Console.ReadKey or commands like that just don't work. Any other way to stop console from closing?
let x = 20
let y = x = 20
printf "is x 20? %d" y
System.Console.ReadKey

you need to do
System.Console.ReadKey() |> ignore
at the end to require a key press to exit - the () is to actually call the function and |> ignore is because you don't care about the result

Related

How to extract only a part of "For Each" loop in ASP/VBScript [duplicate]

This question already has answers here:
Looping through a Scripting.Dictionary using index/item number
(4 answers)
Closed 3 years ago.
I was using this code to extract all items in a RSS file.
For Each item In r.items.items
response.write(item.title)
Next
Each RSS file have 100 items and now I want to show only child numbers from 10 to 20. I dont know how to extract exact items. I have tried the following code:
For x = 10 To 20
response.write(r.items.items(x).title)
Next
but I got an error:
Object not a collection: 'items'
Check the type of r.items.items. I'd suspect that it's enumerable, but doesn't allow indexed access to its elements. You can work around that by using a For Each loop with a custom counter.
i = 0
For Each item In r.items.items
If i > 20 Then
Exit For
ElseIf i >= 10 Then
response.write(item.title)
End If
i = i + 1
Next

Ruby chaining methods with if-statement [duplicate]

This question already has an answer here:
One line if statement in Ruby
(1 answer)
Closed 8 years ago.
I have the following:
def method(integer)
a = 3+integer
a += 10 if "one"<"another"
end
Can I write it in one line somehow with chaining methods?
Something like a = 3+f += 10 if "one"<"another"?
You could do it in one line using the ternary operator:
def method(integer)
a = integer + ("one"<"another" ? 13 : 3)
end
Make sure you don't hurt the readability of the code when you do that, though.
Since and or && both use short-circuit evaluation, you could use:
(a = 3+integer) and ("one"<"another") and (a += 10)
It says in 'Using “and” and “or” in Ruby':
and is useful for chaining related operations together until one of them returns nil or false
Another way of thinking about and is as a reversed if statement modifier
a= 3+ integer + ("one"<"another" ? 10 : 0)
3+ integer will add 3 to integer value and ("one"<"another" ? 10 : 0) will return 10 if condition is true otherwise will return 0.

Ruby ++ not work? [duplicate]

This question already has answers here:
No increment operator (++) in Ruby? [duplicate]
(3 answers)
Closed 9 years ago.
Using Ruby, I can't seem to get the following to work:
a = 1
a++
The above line works in irb but doesn't work when I compile from file.
Is there anything I missed out? I'm using Ruby 2.0.
Ruby has no pre/post increment/decrement operator. For instance, x++ or x-- will fail to parse. More importantly, ++x or --x will do nothing! In fact, they behave as multiple unary prefix operators: -x == ---x == -----x == ...... To increment a number, simply write x += 1
Ruby doesn't have ++ or -- operators but += and -= accomplish the same thing. Try using the += notation like this:
a = 1
a+= 1
#=> 2
Here is a good reference list of valid ruby operators.

What makes this F# so slow in VS2012?

In Visual Studio 2012, why would this code execute so quickly in the interactive mode and so slowly when run as a console application? I have a fast computer, but I can count to 4 before the function completes at run-time and not even to 1 in the interactive studio window.
The other part that irks me about this is that when I test other people's F# code for Project Euler #4, they all run fine. So it leaves me to believe that there is something about this code that is not optimal. (and it was so neat and clean too >:P)
let reverse(digits:string) =
digits.ToCharArray() |> Array.rev |> System.String.Concat
let isPalindrome(number:int) =
let text = number.ToString()
if text.Length % 2 = 0 then
text = reverse(text)
else
false
let palindromes(floor:int, ceiling:int) =
seq {
for i1 = floor to ceiling do
for i2 = floor to ceiling do
let result = i1 * i2
if isPalindrome result then
yield result
}
let run =
palindromes(100, 999)
|> Seq.max
SUMMARY
For the sake of posterity, I'll list the most effective performance changes.
Removing Concat and creating a new string instead.
Retaining only the largest palindrome instead of collecting of all of them
Replacing the string version of reverse with a computational reverse
It still doesn't explain my original issue. But it's is so negligible now, it's hard to convince myself to spend any more time on it. I appreciate everyone's input. Thanks!
If you compile the project on Release mode (Optimize code option turned on), you can hardly see the difference between running the program and executing in F# Interactive.
If you have read other people's versions, you can see that checking isPalindrome can be done directly on numbers. However, a quick fix still using String:
let reverse(digits:string) =
System.String (digits.ToCharArray() |> Array.rev)
Note that string concatenation is slow compared to a String constructor call.
As #wsanville said, odd-length numbers could be palindrome too:
let isPalindrome(number:int) =
let text = number.ToString()
text = reverse(text)
In palindromes function, execution time can be cut down by half by iterating i2 starting from i1:
let palindromes(floor:int, ceiling:int) =
seq {
for i1 = floor to ceiling do
for i2 = i1 to ceiling do
let result = i1 * i2
if isPalindrome result then
yield result
}
let run =
palindromes(100, 999)
|> Seq.max
With these simple optimizations, your code run 5x faster on my machine.
The Question is quite old, and I have the same problem before
Console apps in F#(or any F# apps), loads the "Fsharp.Core.dll" file during execution,
So in order to have a faster exe file do the following.
1.)Build using "Release" mode, (say Hello.exe)
2.)Merge the file "Fsharp.Core.dll" to your exe file
Using ILMERGE(http://www.microsoft.com/en-us/download/details.aspx?id=17630)
-Open Command Prompt and CD to "Release Folder"
ILMERGE Hello.exe Fsharp.Core.dll /out:Hello2.exe
Try to run Hello2.exe, and Check if it loads faster.

Complex IF THEN statements in VB6 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Does VB6 short-circuit complex conditions?
I am curious about how IF statements are executed in VB6.
For example if I have the statement
If x And y Then
'execute some code
End If
Does the code move on if x is not true? Or does it go ahead and evaluate y even though there is no logical point?
Another example
If x Or y Then
'execute some code
End If
Does the code continue and evaluate y if x is true?
EDIT:
Is there a way to avoid nested IF statements if I want to evaluate very complex conditions and I don't want to waste CPU time?
What you are describing is short circuiting logic, and VB6 doesn't have it...
For example, in VB.Net you might write
If x AndAlso y then...
In this case y is not tested if x turns out to be false.
In your VB6 example, you'll get a Object or With block variable not set error if you try something such as:
Dim x as Object
If Not x Is Nothing And x.y=1 Then
Since object x has not been instantiated.
An unwieldy or-like statement that exhibits short circuiting behaviour:
select case True
case a(), b(), c()
'//if a returns true b & c are not invoked, if b returns true a & b were invoked
case else
...
To answer your edit - avoiding nested IF statements, you can use Select Case, covered in the latter half of this article.
Code snippet from the article:
Select Case strShiftCode
Case "1"
sngShiftRate = sngHourlyRate
Case "2"
sngShiftRate = sngHourlyRate * 1.1
Case "3"
sngShiftRate = sngHourlyRate * 1.5
Case Else
Print "Shift Code Error"
End Select

Resources