Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Type inference in F#

I have recently been to a short F# presentation at HiQ in Arboga. One of the things that got mentioned was type inference. Both Visual Basic and C# have type inference in their current version, but F# takes this one step further. This approach will produce two integers in VB when Option Infer is set to On:

Dim X = 10
Dim Y = 20

This is the F# version of the above code:

let mutable x=10
let mutable y=20

But when it comes to function declarations, VB is not support type inference anymore. This will not compile:

Private Function DoSomething(ByVal X, ByVal Y)
   Return X
End Function

You will need to declare the input types and the return type, like so:

Private Function DoSomething(ByVal X As Integer, ByVal Y As _
   Integer) As Integer
   Return X
End Function

In F#, type inference will work all the way. This is the F# version of the DoSomething function, with type inference:

let DoSomething x y = x

The DoSomething function can be called using any arguments. Type inference will take care of the argument and return typing. Once the function is called, the compiler knows what types the function will handle. Therefore, you can call the function using strings or integers but not a combination. Here, z1 will be a string and z2 will be an integer. This is the complete code listing:

let mutable x=10
let mutable y=20

let DoSomething x y = x
let z1 = DoSomething "winsoft" "rocks"
let z2 = DoSomething 10 20

Now, this is strange: If the types interact with each other, the first call will determine the types of the input and output. If I change my code, so that the function DoSomething returns x + y, only the first call will be accepted.

let mutable x=10
let mutable y=20

let DoSomething x y = x + y
let z1 = DoSomething "winsoft" "rocks"
//let z2 = DoSomething 10 20

The second call will fail, because the compiler in this case only provides a string version of the DoSomething function.

My bits on evolution, part 2

Some of my bits for the Swedish Skeptic Podcast, Skeptikerpodden, on evolution and creationism, in Swedish.

Några av mina bidrag till svenska Skeptikerpodden om evolution och kreationism, på svenska.

4. Flodmyter (från Skeptikerpodden 25/5 2010)

5. Evolutionsteorins tillämpningar (från Skeptikerpodden 7/6 2010)

6. Artbildning (från Skeptikerpodden 7/7 2010)

FöregåendeNästa

Monkeybone: The Spline instruction

Using notepad
The Spline instruction has syntax similar to the Line instruction when named arguments are used, but each value has a magnet attached to it. The line will bend to the magnet. This will draw a red line on a black image, from the upper left to the lower right:

Line #ff0000 X1:0 Y1:0 X2:200 Y2:200

This will draw a green line, also from the upper left to the lower right, but it will be bent, like an S (but mirrored).

Spline #00ff00 X1:0+100 Y1:0+0 X2:200-100 Y2:200+0

Not how X1 has its value set to 0 and its magnet set to +100. This means that the curve will bend off 100 pixels to the left. Note that I don’t want to use the magnet on Y1, so I just add +0. I want X2 to bend 100 pixels to the left, so -100 is added.

This is a working example:

//Create a black picture.
Clear 200x200 #000000

//Set line thikness.
Set Line Thikness To 3

//Draw a red line.
Line #ff0000 X1:0 Y1:0 X2:200 Y2:200

//Draw a green spline.
Spline #00ff00 X1:0+100 Y1:0+0 X2:200-100 Y2:200+0

And this is the result:

Using the .NET library
If you are using the .NET library, this Visual Basic code below, the output will be a .MOB file with exactly the same image. This requires a reference to Monkeybone.dll.

Dim OutputPath As String = System.IO.Path.Combine( _
   System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop), _
   "Spline.mob")
Using Sw As New Monkeybone.MonkeyboneWriter(OutputPath, 200, 200, _
   Drawing.Color.Black)

   'Set line thikness to 3.
   Sw.WriteLineThikness(3)

   'Draw a red line across the picture.
   Dim Line As New Monkeybone.Instructions.Line(0, 0, 200, 200)
   Line.Color = System.Drawing.Color.FromArgb(255, 0, 0)
   Sw.WriteLine(Line)

   'Draw a green spline
   Dim Spline As New Monkeybone.Instructions.Spline(0, 100, 0, 0, 200, _
      -100, 200, 0)
   Spline.Color = System.Drawing.Color.FromArgb(0, 255, 0)
   Sw.WriteLine(Spline)

End Using

Using the Monkeybox control
The Monkeybox control (resides in Monkeybox.dll) is capable of displaying the image on screen and exporting the image to a .MOB file. Given that you are writing a Windows Forms application in Visual Basic, and you have a Monkeybox control named MBox1 on your form, this code will produce the exact same image as shown above:

Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) _
   Handles Me.Shown
   'Size is normaly set in the designer, but here I set it manually.
   MBox1.Width = 200
   MBox1.Height = 200

   'Set the background color and the line thikness.
   MBox1.BackColor = Color.Black
   MBox1.SetLineThikness(3)

   'Draw a red line.
   MBox1.AddLine(Color.FromArgb(255, 0, 0), 0, 0, 200, 200)

   'Draw a green spline.
   MBox1.AddSpline(Color.FromArgb(0, 255, 0), 0, 100, 0, 0, 200, -100, 200, 0)

   'Redraw is not trigged by adding and instruction.
   MBox1.Invalidate()
End Sub

Synchronizing subtitles using the SRT Tool

My aim with SRT Tool is to make synchronization easy. Imagine a scenario where you have an Xvid movie that you want to view. The source might be an imported DVD or something, the point is that you don’t have the subtitles for your particular language, so you download it in SRT format from the web. The usual way to connect a SRT file with an AVI file is to place the files in the same directory and to give the two files the same filename, making the file ending the only difference in the name. Now, when you start the movie (let’s say in VLC) you notice that the subtitles are out of sync. This is how you fix that:

Start the movie. Make a note when the first line is being said. Hour, minute, second and millisecond. Also, write down when the last line is being said. Close your media player.

Start SRT Tool and open the SRT file.

The text file might begin or end with information about who wrote the subtitles or perhaps who ripped it. This has to be deleted, so that the first and the last item of the subtitle file is movie translation (of dialog or other). Let’s say that you want to delete the last item. Do this by pressing End on your keyboard, followed by clicking Delete current in the Edit menu.

Click on the time stamp labeled “First in” to be able to edit it. Correct it with the time you wrote down. Click OK.

Do the same with time stamp labeled “Last in”. Correct it and click OK.

Finally, click Save in the File menu to save your changes to the SRT file. Now, when you start the movie, the subtitles are displayed correctly in sync with the dialogs.

Categories: Programs

Tags: SRT Tool



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips