Why I get the error when trying to do this function:
let compile_file path use_asms output =
use pro = new FSharpCodeProvider()
let opt = CompilerParameters(use_asms, output)
let res = pro.CompileAssemblyFromFile(opt, path) // <-- Error
if res.Errors.Count = 0
then Some(FileInfo(res.PathToAssembly))
else None
Error-code: -2147467259
Error: cant find the file specified
Now I'm trying two broken implementations:
type System.CodeDom.Compiler.ICodeCompiler with
member this.CompileAssemblyFromFile
(options:CompilerParameters,fileName:string) : CompilerResults =
this.CompileAssemblyFromFileBatch(options, [|fileName|])
let compile_file str assemblies output =
let pro = new System.CodeDom.Compiler.CodeCompiler()
let opt = CompilerParameters(assemblies, output)
let res = pro.CompileAssemblyFromFile(opt, str)
if res.Errors.Count = 0 then
Some(FileInfo(res.PathToAssembly))
else None
let compile_file2 path use_asms output =
use pro = new FSharpCodeProvider()
let opt = CompilerParameters(use_asms, output)
let res = pro.CompileAssemblyFromFile(opt, path)
if res.Errors.Count = 0
then Some(FileInfo(res.PathToAssembly))
else None
Related
i have big trouble parsing MIDIRawData (SysEx Data) out of a midi-event of a MusicTrack (XCode8.1, iOS10.1)
I managed to parse MidiNoteMessages and MidiChannelMessages with the following code (Swift3):
(eventType and eventData was extracted with MusicEventIteratorGetEventInfo…)
switch eventType {
case kMusicEventType_MIDINoteMessage:
let temp = eventData?.bindMemory(to: MIDINoteMessage.self, capacity: 5)
let channel = temp!.pointee.channel
let note = temp!.pointee.note
let vel = temp!.pointee.velocity
let relVel = temp!.pointee.releaseVelocity
let duration = temp!.pointee.duration
case kMusicEventType_MIDIChannelMessage:
let temp = eventData?.bindMemory(to: MIDIChannelMessage.self, capacity: 4)
let status = temp!.pointee.status & 0b11110000
let channel = temp!.pointee.status & 0b0000111
let data1 = temp!.pointee.data1
let data2 = temp!.pointee.data2
let reserved = temp!.pointee.reserved
In the following case i get the right length of the data, but data itself just consists of the first entry of the SysEx Message
case kMusicEventType_MIDIRawData:
debug.printDebugText(text: "kMusicEventType_MIDIRawData", ToConsole: debugViewGlobal)
let raw = eventData?.bindMemory(to: MIDIRawData.self, capacity: 2)
let length = raw!.pointee.length
let data = raw!.pointee.data // (should be of Type (UInt8)… but is just one UInt8 Value
How can i get the rest of the SysEs Message ?
How do i have to handle (UInt8) ?
Thank You,
Sebastian
I'm trying to create a pipeline state in swift and the app crashes with SIGABRT. I've put the call to newRenderPipelineStateWithDescriptor within a try catch block. Why is it not caching it?
Here's the code,
let defaultLibrary = device.newDefaultLibrary()!
let fragmentProgram = defaultLibrary.newFunctionWithName("passThroughFragment")!
let vertexProgram = defaultLibrary.newFunctionWithName("passGeometry")!
// check TexturedVertex
let vertexDesc = MTLVertexDescriptor()
vertexDesc.attributes[0].format = .Float3
vertexDesc.attributes[0].offset = 0
vertexDesc.attributes[0].bufferIndex = 0
vertexDesc.attributes[1].format = .Float3
vertexDesc.attributes[0].offset = sizeof(Vec3)
vertexDesc.attributes[0].bufferIndex = 0
vertexDesc.attributes[2].format = .Float2
vertexDesc.attributes[0].offset = sizeof(Vec3) * 2
vertexDesc.attributes[0].bufferIndex = 0
vertexDesc.layouts[0].stepFunction = .PerVertex
vertexDesc.layouts[0].stride = sizeof(TexturedVertex)
let pipelineStateDescriptor = MTLRenderPipelineDescriptor()
pipelineStateDescriptor.vertexFunction = vertexProgram
pipelineStateDescriptor.fragmentFunction = fragmentProgram
//pipelineStateDescriptor.vertexDescriptor = vertexDesc
pipelineStateDescriptor.colorAttachments[0].pixelFormat = view.colorPixelFormat
pipelineStateDescriptor.colorAttachments[0].blendingEnabled = false
pipelineStateDescriptor.sampleCount = view.sampleCount
do {
// SIGABRT will happen here when enabling .vertexDescriptor = vertexDesc
try pipelineState = device.newRenderPipelineStateWithDescriptor(pipelineStateDescriptor)
} catch let error {
print("Failed to create pipeline state, error \(error)")
}
The code above doesn't crash, until I enable this line,
pipelineStateDescriptor.vertexDescriptor = vertexDesc
To give a bit of background, my vertex shader used to receive a packed_float4 buffer as input but I'm trying to update it to a struct, as defined in the MTLVertexDescriptor above.
The frustrating thing is that the app just crashes without giving any hints of what's wrong.
Is there any way of get error messages when calling device.newRenderPipelineStateWithDescriptor?
Edit:
This fixes the crash,
vertexDesc.attributes[0].format = .Float3
vertexDesc.attributes[0].offset = 0
vertexDesc.attributes[0].bufferIndex = 0
vertexDesc.attributes[1].format = .Float3
vertexDesc.attributes[1].offset = sizeof(Vec3)
vertexDesc.attributes[1].bufferIndex = 0
vertexDesc.attributes[2].format = .Float2
vertexDesc.attributes[2].offset = sizeof(Vec3) * 2
vertexDesc.attributes[2].bufferIndex = 0
But apparently there's no way to make newRenderPipelineStateWithDescriptor to throw an exception because of that at the moment.
i am facing one issue about how to split a number and string in same String and display it. To be very clear lets take an example i have an string like,
myString = "MH04ED7897"
i want to my output like this
outPut = MH-04-ED-7897
I am new in Swift. try this
let startIndex = number.startIndex.advancedBy(4)
let lastStr:String = number.substringFromIndex(startIndex)
let InitialStr:String = number.substringToIndex(startIndex)
NSLog("%# %#", lastStr,InitialStr)
let startInitialIndex = InitialStr.startIndex.advancedBy(2)
let InitialStart:String = InitialStr.substringToIndex(startInitialIndex)
let Initialfinish:String = InitialStr.substringFromIndex(startInitialIndex)
print("initalStart :\(InitialStart)")
print("Initialfinish :\(Initialfinish)")
let startlastIndex = lastStr.endIndex.advancedBy(-4)
let lastStart:String = lastStr.substringFromIndex(startlastIndex)
let replaced = lastStr.stringByReplacingOccurrencesOfString(lastStart, withString: "")
print("replace:\(replaced)")
// let lastFinish:String = lastStr.substringToIndex(startfirstIndex)
print("lastStart :\(lastStart)")
you get output like
I can't get this F# code to read and update Content Control text fields inside Word documents.
The second function does absolutely nothing and the first one produces this error: An unhandled exception of type 'System.InvalidOperationException' occurred in System.Core.dll
Additional information: Sequence contains no elements
namespace OpenXML
open DocumentFormat.OpenXml
open DocumentFormat.OpenXml.Packaging
open DocumentFormat.OpenXml.Wordprocessing
open System.Linq
// Add the DocumentFormat.OpenXml assembly
// Add the WindowsBase assembly
module public Word =
let query_plain_text_content_control document_path_and_file_name content_control_tag =
use theDoc = WordprocessingDocument.Open((document_path_and_file_name :string), true)
let mainPart = theDoc.MainDocumentPart
let block = mainPart.Document.Body.Descendants<SdtElement>().Where(fun r -> r.SdtProperties.GetFirstChild<Tag>().Val = content_control_tag).Single()
let t = block.Descendants<Text>().FirstOrDefault()
t.Text
let update_plain_text_content_control document_path_and_file_name content_control_tag new_text = async {
use theDoc = WordprocessingDocument.Open((document_path_and_file_name :string), true)
let mainPart = theDoc.MainDocumentPart
let block = mainPart.Document.Body.Descendants<SdtElement>().Where(fun r -> r.SdtProperties.GetFirstChild<Tag>().Val = content_control_tag).Single()
let t = block.Descendants<Text>().FirstOrDefault()
t.Text <- new_text
mainPart.Document.Save() |> ignore
}
Seems to work fine, with a couple of tweaks:
#r#"DocumentFormat.OpenXml.dll"
#r"WindowsBase.dll"
open DocumentFormat.OpenXml
open DocumentFormat.OpenXml.Packaging
open DocumentFormat.OpenXml.Wordprocessing
open System.Linq
// Add the DocumentFormat.OpenXml assembly
// Add the WindowsBase assembly
module public Word =
let query_plain_text_content_control document_path_and_file_name content_control_tag =
use theDoc = WordprocessingDocument.Open((document_path_and_file_name :string), true)
let mainPart = theDoc.MainDocumentPart
let block = mainPart.Document.Body.Descendants<SdtElement>().Where(fun r -> r.SdtProperties.GetFirstChild<Tag>().Val.ToString() = content_control_tag).Single()
let t = block.Descendants<Text>().FirstOrDefault()
t.Text
let update_plain_text_content_control document_path_and_file_name content_control_tag new_text = async {
use theDoc = WordprocessingDocument.Open((document_path_and_file_name :string), true)
let mainPart = theDoc.MainDocumentPart
let block = mainPart.Document.Body.Descendants<SdtElement>().Where(fun r -> r.SdtProperties.GetFirstChild<Tag>().Val.ToString() = content_control_tag).Single()
let t = block.Descendants<Text>().FirstOrDefault()
t.Text <- new_text
mainPart.Document.Save() |> ignore
}
let oldtext = query_plain_text_content_control #".\text.docx" "ctrltag"
let update = update_plain_text_content_control #".\text.docx" "ctrltag" "new text"
Async.RunSynchronously(update)
let newtext = query_plain_text_content_control #".\text.docx" "ctrltag"
.. on a document containing a single plaintext content control, with tag of 'ctrltag', with content 'old text', I get:
val oldtext : string = "Old text"
val update : Async<unit>
val newtext : string = "new text"
Without calling .ToString() on 'r.SdtProperties.GetFirstChild().Val', I got this error:
The type 'string' is not compatible with the type 'StringValue'.
Perhaps there is a problem with your document? The error you are getting would seem to suggest that there are no content controls with the specified Tag.
I read on stackoverflow that easiest way to convert a DateTime variable back to Excel date was simply to do:
let exceldate = int(DateTime)
Admittedly this was in c# and not f#. This is supposed to work as the decimals represent time and int part represents date. I tried this and f# comes back with the error:
The type 'DateTime' does not support a conversion to the type 'int'
So how do I convert back to excel date?
More specificly, I m trying to create a vector of month 1st for a period between start date and end date. Both vector output and start date and end date are floats, i.e. excel dates. Here my clumsy first attempt:
let monthlies (std:float) (edd:float) =
let stddt = System.DateTime.FromOADate std
let edddt = System.DateTime.FromOADate edd
let vecstart = new DateTime(stddt.Year, stddt.Month, 1)
let vecend = new DateTime(edddt.Year, edddt.Month, 1)
let nrmonths = 12 * (edddt.Year-stddt.Year) + edddt.Month - stddt.Month + 1
let scaler = 1.0 - (float(stddt.Day) - 1.0) / float(DateTime.DaysInMonth(stddt.Year , stddt.Month))
let dtsvec:float[] = Array.zeroCreate nrmonths
dtsvec.[0] <- float(vecstart)
for i=1 to (nrmonths-1) do
let temp = System.DateTime.FromOADate dtsvec.[i-1]
let temp2 = temp.AddMonths 1
dtsvec.[i] = float temp2
dtsvec
This doesnt work because of the conversion issue and is rather complicated and imperative.
How do I do the conversion? How can I do this more functionally? Thanks
Once you have the DateTime object, just call ToOADate, like so:
let today = System.DateTime.Now
let excelDate = today.ToOADate()
So your example would end up like so:
let monthlies (std:float) (edd:float) =
let stddt = System.DateTime.FromOADate std
let edddt = System.DateTime.FromOADate edd
let vecstart = new System.DateTime(stddt.Year, stddt.Month, 1)
let vecend = new System.DateTime(edddt.Year, edddt.Month, 1)
let nrmonths = 12 * (edddt.Year-stddt.Year) + edddt.Month - stddt.Month + 1
let scaler = 1.0 - (float(stddt.Day) - 1.0) / float(System.DateTime.DaysInMonth(stddt.Year , stddt.Month))
let dtsvec:float[] = Array.zeroCreate nrmonths
dtsvec.[0] <- vecstart.ToOADate()
for i=1 to (nrmonths-1) do
let temp = System.DateTime.FromOADate dtsvec.[i-1]
let temp2 = temp.AddMonths 1
dtsvec.[i] = temp2.ToOADate()
dtsvec
In regards to getting rid of the loop, maybe something like this?
type Vector(x: float, y : float) =
member this.x = x
member this.y = y
member this.xDate = System.DateTime.FromOADate(this.x)
member this.yDate = System.DateTime.FromOADate(this.y)
member this.differenceDuration = this.yDate - this.xDate
member this.difference = System.DateTime.Parse(this.differenceDuration.ToString()).ToOADate
type Program() =
let vector = new Vector(34.0,23.0)
let difference = vector.difference